views:

22

answers:

1

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.

A: 

Figured it out. I forgot the BindingFlags.Instance flag.

The strange thing is that

ConstructorInfo constructor = typeof(Test).GetConstructor(System.Type.EmptyTypes);

returns null. Is it defective?

Qwertie