0

I have an abstract base-class and several classes deriving from that. The base-class has - against all conventions - some constructors, which I'd love to use. I'm now writing an extension method, which takes a generic type argument. This type is the return type:

public static T Create<T>(string key) where T : Baseclass { }

Now, I want to access Baseclass' constructor such as:

var item = new T(key);

Where the base-class defines the corresponding constructor.

I know, its probably a pretty bad design and there are work-arounds using reflection (naive way) or delegate-functions or putting the constructor-call outside the Create()-call.

I'm just curious why the C#-Compiler does not allow the access to the constructor of a generic type which is defined in the abstract base class, from which the generic type must derive because of the generic type constraints.

There are several questions, nearly similar, but all of them care with constraining the generic type the constructor, in my case the constraint is the class-inheritance.

Patrik
  • 1,355
  • 12
  • 22

1 Answers1

0

Well, firstly you are missing the new() constraint which allows the construction of a generic type i.e.

public static T Create<T>(string key) where T : Baseclass, new() 
{
    ...
}

However, given you need to pass arguments to the constructor, that only results in replacing one error for another. One way around this would be to avoid constructor injection (if possible) and go for parameter injection instead

var item = new T();
item.Key = key;

If it has to be constructor injection, then as you mentioned, you are pretty much limited to using Reflection.

James
  • 80,725
  • 18
  • 167
  • 237
  • Thats the point I have been. I know that the compiler does not regard the constructors in the base class, but of course other methods and properties. Actually, I already used a different approach (I call the constructor outside of `Create`, pass the instance instead of the key and dispose it if unnecessary). The interesting question is: Why has C# such limitation? Practical issues or real conceptual problems? – Patrik Apr 28 '15 at 15:24
  • @PatrikEckebrecht it's not so much the fact it can't see the *base* class constructor, it's more to do with the fact that the compiler just doesn't support calling generic, parameterized, constructors. As for the *why*, see Eric Lipperts [answer](http://stackoverflow.com/questions/9741211/restricting-a-generic-type-parameters-to-have-a-specific-constructor/9741812#9741812) on the topic. – James Apr 28 '15 at 15:34
  • Eric Lippert is stating there, that it is a bad idea to support any method signature stuff in generic type arguments constraints. That's not what I'm asking for: I don't want to define the constructor in the constraint but in the base class. As he states, a constructor is a method, why does the compiler allow access to all methods but constructors when using the generic type. According to his answer, it actually _should_ be possible to call the constructor as I suggested. – Patrik Apr 28 '15 at 16:18
  • The last comment is actually giving the insight. The problem would be to generate IL-Code for actual constructor-call. Seemingly the call to a method, defined in `Baseclass` is accessable for the IL-Body of the calling method, but not the call to a constructor. – Patrik Apr 28 '15 at 16:29