This session is based on the previous session - Part 53 Reflection in C#. Please watch part 53, before continuing with this session.
In this session we will develop a simple winforms application. Please design the form as shown in the image below.
In this session we will develop a simple winforms application. Please design the form as shown in the image below.
Part 54 - C# Tutorial - Reflection Example
In the properties window
Set the Name of the text box to txtTypeName
Set the Name of the button to btnDiscoverTypeInformation
Set the Name of the list boxes, to lstMethods, lstProperties, and lstConstructors
Now double click the button control to generate the event handler.
Copy and paste the following code in the button click event handler (btnDiscoverTypeInformation_Click).
string TypeName = txtTypeName.Text;
Type T = Type.GetType(TypeName);
lstMethods.Items.Clear();
lstProperties.Items.Clear();
lstConstructors.Items.Clear();
MethodInfo[] methods = T.GetMethods();
foreach (MethodInfo method in methods)
{
lstMethods.Items.Add(method.ReturnType.Name + " " + method.Name);
}
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
lstProperties.Items.Add(property.PropertyType.Name + " " + property.Name);
}
ConstructorInfo[] constructors = T.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
lstConstructors.Items.Add(constructor.ToString());
}
Run the application and enter the type name for which you want to find type information. For example, if you enter System.Console, you should see the list of methods, properties and constructors.