Friday, December 09, 2005

iterate all properties in a class

if refdata is an object of class ReferralData, and we'd like to list all properties and its value. following code should work.

PropertyDescriptorCollection pc = TypeDescriptor.GetProperties(refdata);
foreach(PropertyDescriptor p in pc)
{
p.Name //get name of this property
p.GetValue(refdata) //get value of this property,
//note we pass the object into getvalue function
}