PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myObject); foreach (PropertyDescriptor prop in properties) { Console.WriteLine(prop.Name + ": " + prop.PropertyType.Name); }
public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class PersonCustomTypeDescriptor : CustomTypeDescriptor { public PersonCustomTypeDescriptor(ICustomTypeDescriptor parent) : base(parent) { } public override PropertyDescriptorCollection GetProperties() { PropertyDescriptorCollection props = base.GetProperties(); props.Add(new MyPropertyDescriptor("FullName", typeof(string))); return props; } public class MyPropertyDescriptor : PropertyDescriptor { public MyPropertyDescriptor(string name, Type type) : base(name, null) { this.PropertyType = type; } public override bool CanSeriailize { get { return true; } } public override object GetValue(object component) { Person p = (Person)component; return p.FirstName + " " + p.LastName; } public override void SetValue(object component, object value) { string[] names = value.ToString().Split(' '); Person p = (Person)component; p.FirstName = names[0]; p.LastName = names[1]; } } }This example shows how to extend a custom class with TypeDescriptor, by creating a custom descriptor that adds a FullName property to the Person class. It also defines a custom property descriptor that returns the concatenation of the FirstName and LastName properties as the value of the FullName property. This example is not associated with any particular library, but requires the System.ComponentModel namespace.