This is a mirror of official site: http://jasper-net.blogspot.com/

Working with TypeDescriptor in C#

| Sunday, March 27, 2011
TypeDescriptor is a static sealed class which makes the starting point of the API. It exposes information of the object in terms of Properties, Attributes, Events etc in such a way that it could easily be managed and/or consumed. Even though the basic usage of TypeDescriptor is to get metadata of an object, yet it also exposes features to extend the object on the fly. Let us now discuss few capabilities of Descriptors with a little information about its usage.

TypeDescriptor is used to get information of a Type. To use it, you need to pass a component to its static methods. Lets put an example :

Button b = new Button();

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(b);

EventDescriptorCollection events = TypeDescriptor.GetEvents(b);

foreach(PropertyDescriptor pd in props)
    Console.WriteLine(pd.DisplayName);

foreach(EventDescriptor ed in events)
    Console.WriteLine(ed.Name);

Console.ReadLine();
In the above code, I have just created an object of Button class (you can use any class for this) and got the information (name in this example) from it. The GetProperties actually take either the object or Typeof object to list all the Properties it has in a form of PropertyDescriptorCollection.

Read more: Daily .Net Tips

Posted via email from Jasper-net

0 comments: