BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Reflecting Constructor Information

The seventh part of the Reflection tutorial completes the series of articles that describes the reflection of the individual member types for classes and structures. This article describes the reflection of the available information for constructors.

Obtaining Constructor Information

Over the previous four articles in this tutorial we have considered the reflection of information describing the fields, properties, methods and events contained within classes and structures. The last member type that we will look at is the constructor. This article will be a simple one, as reflecting constructor information is very similar to reflecting over methods. When you reflect methods, you generate one or more MethodInfo objects. MethodInfo inherits from MethodBase, which provides a number of properties that we have seen earlier and a method that allows the parameters of the method to be examined. When reflecting constructors, you obtain ConstructorInfo objects. ConstructorInfo is also a subclass of MethodBase, so allows you to read a matching set of properties and to examine constructor parameters in the same manner.

using System.Reflection;

Obtaining a Single Constructor

The key difference between reflecting methods and constructors is the means by which you obtain the data. As constructors do not have names, when you cannot reflect a single, public constructor by simply passing a name to a method of the System.Type class. Instead, you must identify the desired constructor by specifying the types of its parameters. You can do this by passing an array of Type objects to the GetConstructor method. If a constructor exists that has parameters of the specified types in the same order, a ConstructorInfo object is returned. This is demonstrated in the sample code below, which obtains the Exception constructor that includes a single, string parameter.

Type type = typeof(Exception);
ConstructorInfo ctr = type.GetConstructor(new Type[] { typeof(string) });

Console.WriteLine(ctr); // Void .ctor(System.String)

If your interest lies with the default constructor, which has no arguments, pass an empty Type array to GetConstructor:

Type type = typeof(Exception);
ConstructorInfo ctr = type.GetConstructor(new Type[] { });

Console.WriteLine(ctr); // Void .ctor()

Unlike other member types, you cannot simply add binding flags as a second argument to obtain non-public constructors. A more complex overloaded version of GetConstructor, with four parameters, must be used. I will not describe this overload in detail in this article. For our purposes we simply need to know that we can pass the binding flags as the first argument, the Type array to the third parameter and null as the second and fourth values. This is shown below, where the code reflects a protected constructor that builds an exception from serialized data.

Type type = typeof(Exception);
Type[] paramTypes = new Type[]
{
    typeof(System.Runtime.Serialization.SerializationInfo),
    typeof(System.Runtime.Serialization.StreamingContext)
};
BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
ConstructorInfo ctr = type.GetConstructor(bindingFlags, null, paramTypes, null);

Console.WriteLine(ctr);

/* OUTPUT

Void .ctor(System.Runtime.Serialization.SerializationInfo,
System.Runtime.Serialization.StreamingContext)

*/

Obtaining Multiple Constructors

When you wish to reflect multiple constructors from a single type the process is similar to that of reflecting other member types. If you call GetConstructors with no arguments, all of the public constructors of the type are returned as an array of ConstructorInfo objects.

Type type = typeof(Exception);
ConstructorInfo[] ctrs = type.GetConstructors();

foreach(ConstructorInfo ctr in ctrs)
{
    Console.WriteLine(ctr);
}

/* OUTPUT

Void .ctor()
Void .ctor(System.String)
Void .ctor(System.String, System.Exception)

*/

When you wish to include non-public constructors in the results, or filter the constructors in other ways, you can pass binding flags to the GetConstructors method:

Type type = typeof(Exception);
ConstructorInfo[] ctrs = type.GetConstructors(
    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

foreach(ConstructorInfo ctr in ctrs)
{
    Console.WriteLine(ctr);
}

/* OUTPUT

Void .ctor()
Void .ctor(System.String)
Void .ctor(System.String, System.Exception)
Void .ctor(System.Runtime.Serialization.SerializationInfo,
 System.Runtime.Serialization.StreamingContext)

*/
26 March 2012