-
Jan 31st, 2021, 05:29 AM
#1
Thread Starter
New Member
Reflection simple question
Hello Friends,
How could I retrieve the name of the property that I'm calling from a Func?
Here is my example class:
Code:
Public Class MyObject
Public Property Name As String
Public Property Age As Integer
Public Property Score As Integer
End Class
and here is my Func:
Code:
Dim myFunc As Func(Of MyObject, Integer) = Function(x) x.Age
How to get the "Age" property from myFunc? is it possible?
Thank you!
-
Jan 31st, 2021, 07:19 AM
#2
Re: Reflection simple question
I'm loathe to say that it's impossible because there may be something that I'm not aware of but, as far as I can tell, it's not possible. That property access is part of the implementation of the method, not the signature, which is what Reflection is really about. You can do this:
vb.net Code:
Dim body = myFunc.Method.GetMethodBody()
but a MethodBody object still won't give you what you want. All I can imagine you could do is call GetILAsByteArray and then interpret that, which would not be trivial. Think about it. The implementation of the Lambda could be as complex as you like so what exactly would you expect to be able to do if the return value was calculated using some complex expression involving multiple properties?
-
Jan 31st, 2021, 08:07 AM
#3
Thread Starter
New Member
Re: Reflection simple question
Hi jmcilhinney, thanks for your help.
I'm trying to plot a bunch of stock indicators inside a chart, that's why I need the string name of the property to add each Chart.Series() with the proper Id, but now, I think it's more easier to implement it manualy without tricks.
-
Feb 3rd, 2021, 05:27 PM
#4
Re: Reflection simple question
Would it suffice if you had the ability to list all the properties of an object at runtime? I'm not exactly sure of how you're implementing what you're describing. Like what does your actual code look like. I suspect there is a way to achieve what you want but I'm getting the sense that you're not quite approaching it in the right way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|