{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Using Reflection in .NET: Getting the Class Information
Summary
This article discusses obtaining the information about a particular class inside assembly. The .NET Reflection classes like Assembly, Type, MethodInfo, ConstructorInfo, etc. are used obtain this information.
 
Table of Contents

Namespaces and Classes

Getting the Class Information

Figure 1. Getting the Class Information via Reflection

Code Listing 1. Getting the Class Information via Reflection

Code Discussion

Getting a class

Getting the Constructor information

Getting the Parameters

Properties, Methods, Events, etc.

 

This article discusses getting the class information via reflection; another article discusses getting the assembly related information:

Using Reflection in .NET: Getting the Assembly Information

Namespaces and Classes

The two primary classes Assembly (in System.Reflection namespace) and Type (in System namespace) make the reflection possible in .NET. Once you get a particular class in an assembly, you can unlock the information about constructors, methods, properties, events, etc. by using classes like ConstructorInfo, MethodInfo, PropertyInfo, EventInfo, etc. (all these classes are in the System.Reflection namespace).

Getting the Class Information

The utility shown in Figure 1 is used to get the class information. All the code behind ‘Get Class Details’ button is in this article.

Figure 1. Getting the Class Information via Reflection

Figure 1. Getting the Class Information via Reflection

To get the class details via reflection, the assembly dll and a class name inside it are specified. When the ‘Get Class Details’ button is pressed, the code in Listing 1 gets executed. The code discussion follows.

Code Listing 1. Getting the Class Information via Reflection

/// <summary>
/// Information about a particular class inside an assembly is obtained.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClass_Click(object sender, EventArgs e)
{
    // Load the Assembly and get class details
    try
    {
        // Load the assembly and get the class
        Assembly assembly = Assembly.LoadFile(txtAssembly.Text);
        Type currClass = assembly.GetType(txtClass.Text);

        // Prepare the details
        StringBuilder details = new StringBuilder();
        details.AppendLine(currClass.FullName);

        // Constructors
        details.AppendLine(Environment.NewLine + "Constructors:");
        ConstructorInfo[] constructors = currClass.GetConstructors();

        // For each constructor, make up a signature
        foreach (ConstructorInfo constructor in constructors)
        {
            details.Append(constructor.Name + " (");
            ParameterInfo[] parameters = constructor.GetParameters();
            PrepareParams(details, parameters);

            // Or you can obtain the signature from the ToString()
            // details.AppendLine(constructor.ToString());
        }

        // Properties
        details.AppendLine(Environment.NewLine + "Properties:");
        PropertyInfo[] properties = currClass.GetProperties();
        foreach (PropertyInfo property in properties)
            details.AppendLine(property.Name);

        // Methods
        details.AppendLine(Environment.NewLine + "Methods:");
        MethodInfo[] methods = currClass.GetMethods();
        foreach (MethodInfo method in methods)
        {
            // Preparing the signature in a detailed way
            // Shows the ParameterInfo
            // Uncomment below
            // details.Append(method.ReturnParameter + " " + method.Name + " (");
            // ParameterInfo[] parameters = method.GetParameters();
            // PrepareParams(details, parameters);

            // Obtaining the method signature from ToString()
            details.AppendLine(method.ToString());
        }

        // Events
        details.AppendLine(Environment.NewLine + "Events:");
        EventInfo[] events = currClass.GetEvents();
        foreach (EventInfo evt in events)
            details.AppendLine(evt.Name);

        // Display
        txtDetails.Text = details.ToString();
    }

    catch (Exception exception)
    {
        txtDetails.Text = "Class details could not be obtained. Exception Message: " + exception.Message;
    }
}

/// <summary>
/// Prepares the parameters
/// </summary>
/// <param name="details"></param>
/// <param name="parameters"></param>
private static void PrepareParams(StringBuilder details, ParameterInfo[] parameters)
{
    int paramCount = 1;
    foreach (ParameterInfo parameter in parameters)
    {
        details.Append(parameter.ParameterType + " " + parameter.Name);
        if (paramCount < parameters.Length)
            details.Append(", ");
        paramCount++;
    }
    details.AppendLine(")");
}

Code Discussion

Getting a class

        // Load the assembly and get the class
        Assembly assembly = Assembly.LoadFile(txtAssembly.Text);
        Type currClass = assembly.GetType(txtClass.Text);

By using the LoadFile method from the Assembly class, the assembly dll specified by the user in the txtAssembly text box is loaded. Then the class specified in the txtClass textbox is obtained by using the GetType method in the Assembly class. Technically, this returns any type (interface, enumeration, etc.) – but here a class is shown as an example. The code following needs to be changed a bit if you want to obtain information about other types.

Getting the Constructor information

        // Constructors
        details.AppendLine(Environment.NewLine + "Constructors:");
        ConstructorInfo[] constructors = currClass.GetConstructors();

        // For each constructor, make up a signature
        foreach (ConstructorInfo constructor in constructors)
        {
            details.Append(constructor.Name + " (");
            ParameterInfo[] parameters = constructor.GetParameters();
            PrepareParams(details, parameters);

            // Or you can obtain the signature from the ToString()
            // details.AppendLine(constructor.ToString());
        }

The Type class provides various methods like GetConstructors(), GetMethods(), and so on. These methods return arrays of objects of the classes like ConstructorInfo, MethodInfo, etc.

Now all you have to do is to loop through the individual ConstructorInfo object to get the information like Name, etc. If all you want is a signature, then ToString() method on the ConstructorInfo provides it for you.

Getting the Parameters

Just like you get a list of constructors from GetConstructors() method, you can run a GetParameters() method on an individual constructor or method and obtain a list of parameters.

            ParameterInfo[] parameters = constructor.GetParameters();

And then, as shown below, a method signature can be made by looping through the parameters.

/// <summary>
/// Prepares the parameters
/// </summary>
/// <param name="details"></param>
/// <param name="parameters"></param>
private static void PrepareParams(StringBuilder details, ParameterInfo[] parameters)
{
    int paramCount = 1;
    foreach (ParameterInfo parameter in parameters)
    {
        details.Append(parameter.ParameterType + " " + parameter.Name);
        if (paramCount < parameters.Length)
            details.Append(", ");
        paramCount++;
    }
    details.AppendLine(")");
}

Properties, Methods, Events, etc.

The code in Listing 1 shows how to get information about Properties, Methods, etc. The process is the same – use a Get…() method to obtain a …Info[] array and loop through it to get more information about each type.

Bookmark and Share This

More Articles With Similar Tags
icon-reflection-assembly-details.jpg
This article shows the reflection functionality built into the .NET Framework with the help of a small Windows Forms utility. Here the focus is on getting the assembly information.
Top Tags in Related Articles
assembly classes reflection
About  Contact  Privacy Policy  Site Map