title: Instantiating a class depending of generic parameter type
body: <p>I'm hitting quite a wall right now and can't find a solution to my problem.</p>
  
  <p>I'm trying to select a class to instantiate depending of the type of my generic parameter. If the generic is a class, instantiate a TestClass ; if it's a struct, instantiate a TestStruct ; if it's an enum, instantiate a TestEnum. These three classes inherit from Test.</p>
  
  <p>The goal of these classes is to avoid boxing when doing comparison.</p>
  
  <p>Here is what my code looks like for now:</p>
  
  <pre><code>    public static Test&lt;T&gt; Create()
      {
          return Create(default(T));
      }
  
      private static Test&lt;U&gt; Create&lt;U&gt;(U arg)
          where U : class
      {
          return TestClass&lt;U&gt;.Create();
      }
  
      private static Test&lt;U&gt; Create&lt;U&gt;(U? arg)
          where U : struct
      {
          return CreateStruct(arg);
      }
  
      private static Test&lt;U&gt; CreateStruct&lt;U&gt;(U? arg)
          where U : struct
      {
          if (typeof(U).IsEnum)
          {
              return TestEnum&lt;U&gt;.Create();
          }
          return TestStruct&lt;U&gt;.Create();
      }
  </code></pre>
  
  <p>I get an error in the first Create() function: <em>The type 'T' must be a reference type in order to use it as parameter 'U' in the generic type or method 'Test.Create(U)'</em></p>
  
  <p>Which makes sense, but how can I get it to work? If I remove the constraint on the second function (where U : class), it compiles but will always call this function, even if I pass an int for instance</p>
  
  <p>I'm using Unity, so the C# standard is rather old, which doesn't help...</p>