Using reflection, is it possible to create an instance of a type that inherits from an abstract base class using the abstract base class' constructor? That is, without the inheriting class having a constructor of its own? Somewhat like below, but it throws an error because you cannot create an instance of an abstract class, of course:
abstract class PersonBase
{
string Name;
public PersonBase(string _name) { Name = _name; }
}
class Person : PersonBase
{
}
public static T GetPerson<T>(string name) where T : PersonBase, new()
{
ConstructorInfo info = typeof(T).BaseType.GetConstructor(new Type[]
{ typeof(string) });
object result = info.Invoke(new object[] { name });
return (T)result;
}
You can make this work by doing new T { (assign properties here) } but of course thats not a constructor, and the properties would have to be public, etc.