How to use reflection to get a default constructor?
- by Qwertie
I am writing a library that generates derived classes of abstract classes dynamically at runtime. The constructor of the derived class needs a MethodInfo of the base class constructor so that it can invoke it. However, for some reason Type.GetConstructor() returns null. For example:
abstract class Test
{
public abstract void F();
}
public static void Main(string[] args)
{
ConstructorInfo constructor = typeof(Test).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Public,
null, System.Type.EmptyTypes, null); // returns null!
}
Note that GetConstructor returns null even if I explicitly declare a constructor in Test, and even if Test is not abstract.