I have a base class with many descendants. In the base class a parameter-less constructor is declared. Descendants may declare specialized constructors.
public class A
{
public A()
{
}
}
public class B : A
{
string Stuff { get; set; }
public B(string stuff)
{
Stuff = stuff;
}
}
At some point I want to instantiate a B using the Activator. This instance needs no initialization of it's "Stuff". So I want to use the A's default constructor:
public object InstantiateType(Type type)
{
return Activator.CreateInstance(
type,
BindingFlags.CreateInstance |
BindingFlags.Public |
BindingFlags.NonPublic
);
}
This sample call results in an exception as many other variations on this theme, is it at all possible, and if so how do I instruct the Activator to do so?