Results 1 to 4 of 4

Thread: Reflection simple question

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. 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?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    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.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width