title: Restricting the generic type parameter to System.Enum
body: <p>I have a generic type that should be specified with an <code>Enum</code> type (actually, it's one of several specified enums, but I'll settle for <code>System.Enum</code>).</p>
  
  <p>Of course the compiler balks at code like:</p>
  
  <pre><code>class Generic&lt;T&gt; where T : Enum {}
  </code></pre>
  
  <p>with a <em>"Constraint cannot be special class 'System.Enum'"</em> exception.</p>
  
  <p>The only solution I've been able to come up so far is using the static type initializer to inspect the type parameter and throw an exception if it is not, in fact, an Enum, like this:</p>
  
  <pre><code>class Generic&lt;T&gt; 
  {
    static Generic()
    {
      if (typeof(T).BaseType != typeof(Enum))
        throw new Exception("Invalid Generic Argument");
    }
  }
  </code></pre>
  
  <p>which at least gives me runtime security that it wont we used with a non-enum parameter. However this feels a bit hacky, so is there a better way to accomplish this, ideally with a compile-time construct?</p>