I have a generic class like this:
class MyGenericClass<T> { }
On the other hand I have an aggregator class that I use to obtain singleton instances of MyGenericClass
and of derived classes, where each instance is identified by the concrete class type and by the parameter type. That is, MyGenericClass<int>
is different from MyGenericClass<string>
and from MyDerivedGenericClass<int>
; there is one single instance for each of them.
The main member of the aggregator is the GetInstanceOf
method, which will return the singleton instance of the specified type. The problem is that I want to specify the type as a generic parameter, but I can't find a way to create a restriction of type "the parameter must be of type MyGenericClass (or derived) with any parameter type". In short, I want something like this:
T GetInstanceOf<T>() where T : MyGenericClass<> //This does not compile
So, what can I do? Is there any way to specify such a restriction?
EDIT: The GetInstanceOf<T>
method indeed returns T, not void.