How to get class information using .NET Reflection

By | January 20, 2013

In this article I will explain how to get properties, methods and events of class using .NET Reflection



In this article we will see how to get class or type information of all the types inside System.Windows.Forms assembly. We will list all the properties, methods, and events of the selected class. We will use Reflection to get this information.

 DataGridView Properties Methods Events List


Step:1 Create a new Windows Forms Application and arrange control on Form1 as in the above figure

 New Windows Forms Application

Step:2 Import System.Reflection namespace

using System.Reflection;

Step:3 Write following code in the Load event of Form1


GetAssembly method of Assembly class returns the assembly in which the given class exists. In this case it will return System.Windows.Forms assembly. We can give any class of System.Windows.Forms assembly as a parameter to GetAssembly like Button, CheckBox etc. Similarly we can also get reference to other assemblies like System.Drawing by passing a class inside it like Color.

We can also use GetCallingAssembly method to get System.Windows.Forms assembly that returns assembly of the method of the currently invoked method.

GetTypes method returns all the types inside System.Windows.Forms assembly and is then bound to cbTypes ComboBox as DataSource

Step:4 Write GetTypeDetails method to populate properties, methods and events of the given class


GetProperties, GetMethods and GetEvents methods of Type class returns an array of PropertyInfo, MethodInfo and EventInfo classes respectively that provides information about properties, methods and events of a class. Reflection gives us some more methods like GetConstructors, GetFields, GetMembers, GetInterfaces etc to get more information about a class. We can also use overloaded form of these methods takes a BindingFlags enumeration to filter.

These arrays then bound as data source of the list boxes lbProperties, lbMethods and lbEvents and count of these array is written on three labels to show number of properties, methods and events.

Step:5 Write following code in the Click event of btnGetDetails Button


In Click event of the Button we call GetTypeDetails method that takes a Type as parameter. A Type is decalred as TypeName which is initialize using GetType method of the Type class. It takes three parameters, first is the assembly qualified name of the type which looks like “System.Windows.Forms.ToolStripLabel,System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089“, where type name “System.Windows.Forms.ToolStripLabel” is selected from the ComboBox, second parameter specifies whether to throw an exception when the type is not found and third parameter is used to perform a case sensitive of case insensitive search for the given type. True is for case insensitive.

Leave a Reply

Your email address will not be published. Required fields are marked *