Results 1 to 27 of 27

Thread: [RESOLVED] Dynamically get propeties of an object.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Resolved [RESOLVED] Dynamically get propeties of an object.

    I need help with some code to get the propeties of an object dynamically.

    What i need to do is generate a treeview according to the propeties of an object so when i change the class, because the object is instantiated using the class the treeview should change accordingly.

    basically this is what i want to do.

    Dim PropList() As String = Split(Obj.PropertyList)
    Dim I as integer
    For I = 0 to Ubound(PropList)
    AddTreeViewNode(PropList(I),Ob.Properties(PropList(I))
    Next

    addTreeViewNode is a method that adds nodes to a treeview. What i need is a way of using something similar to Obj.PropertyList and Ob.Propeties (these don't exist obviously)

    Any ideas anybody?

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Dynamically get propeties of an object.

    VB Code:
    1. Dim ObjectProperties() As PropertyInfo = Ob.GetType.GetProperties()

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Thanks for that Merrion but How do i get the values out of them?

    i.e

    dim a as testObjClass
    Dim ObjectProperties() As PropertyInfo = a.GetType.GetProperties()

    how do you do something equavalent to
    dim x() as variant
    for i = 0 to UBound(ObjectPropeties())
    x(i) = a.property(ObjectProperties(i))
    next

  4. #4
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    VB Code:
    1. Dim i as Integer
    2. For i = 0 to ObjectProperties.Length - 1
    3.     Dim propInfo as PropertyInfo = CType(ObjectProperties(i), PropertyInfo)
    4.     'This displays the name in the console
    5.     Console.WriteLine(propInfo.Name.ToString())
    6. Next
    You can pretty much use that concept to add the nodes to your treeview.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    for some reason that doesn't work. I put a tracer and i found that the length of the array is 0 even after
    Dim ObjectProperties() As PropertyInfo = a.GetType.GetProperties()

    which means that it doesn't get the desired value from a.getType.GetProperties()

    Help!!!!!

  6. #6
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    VB Code:
    1. Dim ObjectProperties() As PropertyInfo =
    2. Ob.GetType.GetProperties(BindingFlags.Instance Or BindingFlags.NonPublic)
    Try that and see if you get an array.

    (If you don't already have an 'Imports System.Reflection' statement in your code, I would recommend putting one for the above code to work.)
    Last edited by Kyjan; Oct 10th, 2005 at 10:06 AM. Reason: Correction

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Nope still the same 0 length array!

  8. #8
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    Where are you actually assigning this object to your object variable? Have you made sure that it's actually an object that has properties?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    the object is made out of a class thats has about 10 propeties. And i've actually asigned a value to a property just to make sure.

    This is my code in full.
    VB Code:
    1. Dim a As Patient = New Patient
    2. a.PAT = 10  'PAT is a property of the class patient
    3.  
    4. Dim objectProperties() As System.Reflection.PropertyInfo GetType.GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
    5.  
    6.             Dim i As Integer
    7.  
    8.             For i = 0 To objectProperties.Length - 1
    9.                 Dim propInfo As System.Reflection.PropertyInfo = CType(objectProperties(i), System.Reflection.PropertyInfo)
    10.  
    11.             MsgBox(propInfo.Name.ToString())
    12.             Next

  10. #10
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    VB Code:
    1. Dim objectProperties() As System.Reflection.PropertyInfo =
    2. a.GetType.GetProperties(System.Reflection.BindingFlags.Instance
    3. Or System.Reflection.BindingFlags.NonPublic)
    Try that instead.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Mmm Is it me or isn't it the same line i have already? I've tried copying your code anyway but no luck yet!

  12. #12
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    No, in your code, I don't see you doing a.GetType, I just see you doing a GetType. If you don't use the GetType method on the object you're wanting to get the type of, you won't get any properties back to begin with.

  13. #13
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Dynamically get propeties of an object.

    Although I didn't full understand everyone's posts, I'll risk posting an example:
    VB Code:
    1. Dim o As New TextBox
    2.  
    3.         'Setting up a value for the text property.
    4.         o.Text = "Some text"
    5.  
    6.         'Getting *all* properties.
    7.         Dim props() As System.Reflection.PropertyInfo = o.GetType().GetProperties()
    8.  
    9.         Dim i As Integer
    10.         For i = 0 To props.GetUpperBound(0)
    11.             'Interested in plain properties - I think this means only published properties with get/set.
    12.             If props(i).MemberType = Reflection.MemberTypes.Property Then
    13.                 Try
    14.                     Dim v As Object = props(i).GetValue(o, Nothing)
    15.                     Debug.WriteLine("Property:" + props(i).Name + ", value--->" + Convert.ToString(v))
    16.                 Catch ex As Exception
    17.                     Debug.WriteLine("Oops!")
    18.                 End Try
    19.             End If
    20.         Next

    This will write the properties and their values of a textbox to the debug output.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Dynamically get propeties of an object.

    The problem here:
    Dim objectProperties() As System.Reflection.PropertyInfo =
    a.GetType.GetProperties(System.Reflection.BindingFlags.Instance
    Or System.Reflection.BindingFlags.NonPublic)
    Is that you are asking for only Private members which aren't all that common. I think that was meant to be Public instead of NonPublic.

    Also don't forget about the PropertyGrid control which even allows editing of the properties and takes care of displaying them for you.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Hi I still cant get it to work.

    I tried ntg's example. Funnily enough it works for a text box object. But it doesn't work for the object which is of a class i worte with about 10 propeties in it.

    Anyone know why?

  16. #16
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Dynamically get propeties of an object.

    Please post up the class you wrote with the 10 properties...are they in fact properties (with a get/set) or public members (which are different...)

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Yeah they are public members with no Get / Set. I'd like to keep it that way if its is possible. Is there a way to access the public properties?

  18. #18
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    VB Code:
    1. Dim objectProperties() As System.Reflection.PropertyInfo =
    2. a.GetType.GetProperties(System.Reflection.BindingFlags.Instance
    3. Or System.Reflection.BindingFlags.Public)
    Change it to that instead of the NonPublic.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    nope no luck yet!

  20. #20
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    Wait a minute...

    You just said there were no get/set? A property has to be formatted like this:
    VB Code:
    1. Private _something as String
    2.         Property Something() As String
    3.             Get
    4.                 Return _something
    5.             End Get
    6.             Set(ByVal Value As String)
    7.                 _something = Value
    8.             End Set
    9.         End Property
    If your properties are not set up like that, then none of this code is going to work anyway.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Re: Dynamically get propeties of an object.

    Yeah I figured that.
    But The problem is the object is not derived out of a VB.NET class. So unfortunatley I can't format my class like that.

    So basically what happens is my VB.NET application accepts an CACHE object. And make a copy of that object in VB.NET. Thats why i want to access the properties of that object so that no matter what the content of the CACHE object .NET would be able to handle it.

    So there you go. Thats my problem.

  22. #22
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    I would have to see the class definition for your cache object to help you any further. If you would like to send me your code so I can take a look at it, please PM me and I'll let you know where to send it.

    Kyjan

  23. #23
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Dynamically get propeties of an object.

    If they are just fields and not properties then you can use GetFields or GetMembers instead of GetProperties. The problem is your terminology- if it doesn't have a get/set then it is not a property. It would be helpful to see the code you have so we are all on the same page.

  24. #24
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Dynamically get propeties of an object.

    Quote Originally Posted by Bimalke
    Yeah I figured that.
    But The problem is the object is not derived out of a VB.NET class. So unfortunatley I can't format my class like that.

    So basically what happens is my VB.NET application accepts an CACHE object. And make a copy of that object in VB.NET. Thats why i want to access the properties of that object so that no matter what the content of the CACHE object .NET would be able to handle it.
    If your compiled output can be handled by ILDasm, I would expect that it Reflection should be sufficient for what you want to do.

    Is there any way you can post the DLL from which your class is derived from?

    BTW, is COM Interop involved?
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  25. #25
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Dynamically get propeties of an object.

    I had him send me the code to look at - it's not VB .NET. I'm going to have to assume it's a COM object, but since I've never seen code for one, I can't be sure. I'll leave it up to Bimalke whether or not he wants to put the code up here (in case there's something sensitive about it).

    Might explain why all of our suggestions haven't worked.

    Kyjan

  26. #26
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Dynamically get propeties of an object.

    We don't even really need the code to the class just open in up in the ObjectBrowser window and take a screen shot of that.

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    17

    Thumbs up Re: [RESOLVED] Dynamically get propeties of an object.

    You've all been very helpful guys. Thanks. I've had to find another way around it having realized its not easy to do this with a non VB.NET class involved.

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