Sep 17, 2015

Printing all properties of an Object

To write all the properties and such of an object to the console (or log or to front end), use TypeDescriptor class.

foreach(System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(obj))
{
    string name=descriptor.Name;
    object value=descriptor.GetValue(obj);
    Console.WriteLine("{0}={1}",name,value);
}

Where obj is the class for which properties are to be printed.

No comments: