0

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.

1
  • Typically this is seen as a prime case for a Factory method. Commented May 27, 2011 at 19:18

1 Answer 1

4

The only place it's legal to call an abstract class constructor - whether by reflection or not - is within the constructor of a derived class. So no - as you say, you'd be creating an instance of the abstract type, instead of the concrete type. Imagine if the abstract class had an abstract method, and you called it on whatever the constructor returned - what would that do?

If you can give us more information about what you're trying to achieve, we may be able to help you more.

(Your current example wouldn't even compile, as the default Person constructor doesn't have a parameterless base class constructor to call.)

Sign up to request clarification or add additional context in comments.

6 Comments

I am creating an abstract immutable type with a readonly member. I want this generic 'Get' method to be able to get an instance of any class that derives from that abstract type, using the base constructor. Since its immutable the member has to be assigned in a constructor. Dont want the inheriting types to have a constructor at all, if possible. But I'll go another way.
@Sean: You can't create a type which doesn't have a constructor other than as static class (which can't inherit from anything other than object), and you can't create an instance of just the abstract class.
Well you can have a class that only has public properties, and initialize it like so: new Foo { Property1 = x, Property2 = x }. I guess that class still has a default parameterless constructor. I meant no constructor other than default, I suppose. Anyways readonly only properties cant be initialized that way.
@Sean: That calls the parameterless constructor, or possibly a constructor with optional parameters. It's still calling a constructor. Only a parameterless constructor is fine, but that constructor would have to call into a constructor in your base class. It's still not clear why you're trying to do this. If you can give us the bigger picture, we may well be able to help.
@Sean: You don't need to post the full code necessarily - just give us insight into the bigger picture you're trying to achieve.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.