Does object o contain a member m?

  • Thread starter Thread starter .pd.
  • Start date Start date
P

.pd.

Hello,

Is it possible to use reflection to determine if a given object contains a
member with a specific name and/or type?

I have a control that can be placed on a Panel, a Form or any one of many
derivatives of either. The control is located relative to the width of the
owning object so I need to figure out if the owning object has a member
called "Width".

Can it be done and, if so, how?

Thanks for any help,

..pd.
 
PropertyInfo pi = ctrl.GetType().GetProperty("Width");
if ( pi != null )
{
int Width = (int)pi.GetValue(ctrl, null);
}
 
Alex Feinman [MVP] wrote on Mon 03 Nov 2003 05:52:27p:
PropertyInfo pi = ctrl.GetType().GetProperty("Width");
if ( pi != null )
{
int Width = (int)pi.GetValue(ctrl, null);
}

Aaah - I got pretty close. I was using GetField and, of course, GotNowhere
cuz it's a property.

Thank you!

..pd.
 
GetField would have worked too but you needed some flags specified.
Internally, GetProperty calls to GetField
 
Back
Top