How to use reflection to get a default constructor?
Posted
by Qwertie
on Stack Overflow
See other posts from Stack Overflow
or by Qwertie
Published on 2010-06-13T03:14:42Z
Indexed on
2010/06/13
3:22 UTC
Read the original article
Hit count: 388
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.
© Stack Overflow or respective owner