Enumerating an objects properties and values?

  • Thread starter Thread starter Ben Fidge
  • Start date Start date
B

Ben Fidge

How do you enumerate all the properties of a given object and retrieve each
property's type and current value, the same as in VS.NET's Proprties view?

Thanks

Ben
 
Ben,
How do you enumerate all the properties of a given object and retrieve each
property's type and current value, the same as in VS.NET's Proprties view?

You can do this with Reflection. Something like

foreach ( PropertyInfo pi in obj.GetType().GetProperties() ) {
Console.WriteLine( pi.PropertyType );
if ( pi.GetIndexParameters().Length == 0 )
Console.WriteLine( pi.GetValue( obj, null ) );
}



Mattias
 
Back
Top