-
[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?
-
Re: Dynamically get propeties of an object.
VB Code:
Dim ObjectProperties() As PropertyInfo = Ob.GetType.GetProperties()
-
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
-
Re: Dynamically get propeties of an object.
VB Code:
Dim i as Integer
For i = 0 to ObjectProperties.Length - 1
Dim propInfo as PropertyInfo = CType(ObjectProperties(i), PropertyInfo)
'This displays the name in the console
Console.WriteLine(propInfo.Name.ToString())
Next
You can pretty much use that concept to add the nodes to your treeview.
-
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!!!!!
-
Re: Dynamically get propeties of an object.
VB Code:
Dim ObjectProperties() As PropertyInfo =
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.)
-
Re: Dynamically get propeties of an object.
Nope still the same 0 length array!
-
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?
-
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:
Dim a As Patient = New Patient
a.PAT = 10 'PAT is a property of the class patient
Dim objectProperties() As System.Reflection.PropertyInfo GetType.GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
Dim i As Integer
For i = 0 To objectProperties.Length - 1
Dim propInfo As System.Reflection.PropertyInfo = CType(objectProperties(i), System.Reflection.PropertyInfo)
MsgBox(propInfo.Name.ToString())
Next
-
Re: Dynamically get propeties of an object.
VB Code:
Dim objectProperties() As System.Reflection.PropertyInfo =
a.GetType.GetProperties(System.Reflection.BindingFlags.Instance
Or System.Reflection.BindingFlags.NonPublic)
Try that instead.
-
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!
-
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.
-
Re: Dynamically get propeties of an object.
Although I didn't full understand everyone's posts, I'll risk posting an example:
VB Code:
Dim o As New TextBox
'Setting up a value for the text property.
o.Text = "Some text"
'Getting *all* properties.
Dim props() As System.Reflection.PropertyInfo = o.GetType().GetProperties()
Dim i As Integer
For i = 0 To props.GetUpperBound(0)
'Interested in plain properties - I think this means only published properties with get/set.
If props(i).MemberType = Reflection.MemberTypes.Property Then
Try
Dim v As Object = props(i).GetValue(o, Nothing)
Debug.WriteLine("Property:" + props(i).Name + ", value--->" + Convert.ToString(v))
Catch ex As Exception
Debug.WriteLine("Oops!")
End Try
End If
Next
This will write the properties and their values of a textbox to the debug output.
-
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.
-
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?
-
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...)
-
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?
-
Re: Dynamically get propeties of an object.
VB Code:
Dim objectProperties() As System.Reflection.PropertyInfo =
a.GetType.GetProperties(System.Reflection.BindingFlags.Instance
Or System.Reflection.BindingFlags.Public)
Change it to that instead of the NonPublic.
-
Re: Dynamically get propeties of an object.
-
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:
Private _something as String
Property Something() As String
Get
Return _something
End Get
Set(ByVal Value As String)
_something = Value
End Set
End Property
If your properties are not set up like that, then none of this code is going to work anyway.
-
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.
-
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
-
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.
-
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?
-
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
-
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.
-
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.
:wave: