title: How to specify a generic type as a restriction in a generic method
body: <p>I have a generic class like this:</p>
  
  <pre><code>class MyGenericClass&lt;T&gt; { }
  </code></pre>
  
  <p>On the other hand I have an aggregator class that I use to obtain singleton instances of <code>MyGenericClass</code> and of derived classes, where each instance is identified by the concrete class type and by the parameter type. That is, <code>MyGenericClass&lt;int&gt;</code> is different from <code>MyGenericClass&lt;string&gt;</code> and from <code>MyDerivedGenericClass&lt;int&gt;</code>; there is one single instance for each of them.</p>
  
  <p>The main member of the aggregator is the <code>GetInstanceOf</code> 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 <em>"the parameter must be of type MyGenericClass (or derived) with any parameter type"</em>. In short, I want something like this:</p>
  
  <pre><code>T GetInstanceOf&lt;T&gt;() where T : MyGenericClass&lt;&gt; //This does not compile
  </code></pre>
  
  <p>So, what can I do? Is there any way to specify such a restriction?</p>
  
  <p><strong>EDIT:</strong> The <code>GetInstanceOf&lt;T&gt;</code> method indeed returns T, not void.</p>