Finding out if a property exists in an object

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
I'm not completely new to .net but this is the first time I've had to do this. I am doing a project with alot of polymorphism and i need to determine if a property exists in an object without knowing ahead of time the type of the object.

I have a propertygrid control which takes any number of classes to fill out the propertygrid control with properties.

I have a base class that is trying to see if the propertygrid has a certain parameter to know how to manipulate the screen accordingly which not all classes contain.

Is this possible?

Thanks in advance!
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Thanks for the tip! Got it working:

Dim objl_PROPERTYINFO As System.Reflection.PropertyInfo
objl_PROPERTYINFO = objm_PROPERTYCLASS.GetType().GetProperty("Today")

If Not objl_PROPERTYINFO Is Nothing Then
If objm_PROPERTYCLASS.Today = False Then
objm_REFRESH.Checked = False
objm_TIMER.Enabled = False
Else
objm_REFRESH.Checked = True
objm_TIMER.Enabled = True
End If
End If
 

clamum

Lifer
Feb 13, 2003
26,255
403
126
Recently I had to write a class that implements IComparer to sort a generic List of some business objects, given one of the object's properties, and it used that PropertyInfo class and GetProperty method. I got the snippet of code from somewhere else but this is something that could definitely come in handy. :beer:
 

ASK THE COMMUNITY