title: Generic type checking
body: <p><strong>Is there a way to enforce/limit the types that are passed to primitives?</strong> <em>(bool, int, string, etc.)</em></p>
  <p>Now, I know you can limit the generic type parameter to a type or interface implementation via the <em>where</em> clause. However, this doesn't fit the bill for primitives (AFAIK) because they do not all have a common ground (apart from <em>object</em> before someone says! :P).</p>
  <p>So, my current thoughts are to just grit my teeth and do a big <em>switch</em> statement and throw an <em>ArgumentException</em> on failure.</p>
  <hr />
  <p><strong>EDIT 1:</strong></p>
  <p>Just to clarify:</p>
  <p>The code definition should be like this:</p>
  <pre><code>public class MyClass&lt;GenericType&gt; ....
  </code></pre>
  <p>And instantiation:</p>
  <pre><code>MyClass&lt;bool&gt; = new MyClass&lt;bool&gt;(); // Legal
  MyClass&lt;string&gt; = new MyClass&lt;string&gt;(); // Legal
  MyClass&lt;DataSet&gt; = new MyClass&lt;DataSet&gt;(); // Illegal
  MyClass&lt;RobsFunkyHat&gt; = new MyClass&lt;RobsFunkyHat&gt;(); // Illegal (but looks awesome!)
  </code></pre>
  <hr />
  <p><strong>EDIT 2</strong></p>
  <p>@Jon Limjap - Good point, and something I was already considering. I'm sure there is a generic method that can be used to determine if the type is of a value or reference type.</p>
  <p>This could be useful in instantly removing a lot of the objects I don't want to deal with (but then you need to worry about the structs that are used such as <em>Size</em> ). Interesting problem no? :)</p>
  <p>Here it is:</p>
  <pre><code>where T: struct
  </code></pre>
  <p>Taken from <a href="http://msdn.microsoft.com/en-us/library/d5x73970.aspx" rel="nofollow noreferrer">MSDN</a>.</p>
  <hr />
  <p>I'm curious. Could this be done in .NET 3.x using extension methods? Create an interface, and implement the interface in the extension methods (which would probably be cleaner than a bit fat switch). Plus if you then need to later extend to any lightweight custom types, they can also implement the same interface, with no changes required to the base code.</p>
  <p>What do you guys think?</p>
  <p>The sad news is I am working in Framework 2!! :D</p>
  <hr />
  <p><strong>EDIT 3</strong></p>
  <p>This was so simple following on from <a href="https://stackoverflow.com/questions/8941/generic-type-checking#8956">Jon Limjaps Pointer</a>.. So simple I almost want to cry, but it's great because the code works like a charm!</p>
  <p>So here is what I did (you'll laugh!):</p>
  <h2>Code added to the generic class</h2>
  <pre><code>bool TypeValid()
  {
      // Get the TypeCode from the Primitive Type
      TypeCode code = Type.GetTypeCode(typeof(PrimitiveDataType));
  
      // All of the TypeCode Enumeration refer Primitive Types
      // with the exception of Object and Empty (Null).
      // Since I am willing to allow Null Types (at this time)
      // all we need to check for is Object!
      switch (code)
      {
          case TypeCode.Object:
              return false;
          default:
              return true;
      }
  }
  </code></pre>
  <p>Then a little utility method to check the type and throw an exception,</p>
  <pre><code>private void EnforcePrimitiveType()
  {
      if (!TypeValid())
          throw new InvalidOperationException(
              &quot;Unable to Instantiate SimpleMetadata based on the Generic Type of '&quot; + typeof(PrimitiveDataType).Name + 
              &quot;' - this Class is Designed to Work with Primitive Data Types Only.&quot;);
  }
  </code></pre>
  <p>All that then needs to be done is to call <em>EnforcePrimitiveType()</em> in the classes constructors. Job done! :-)</p>
  <p>The only downside, it only throws an exception at runtime (obviously) rather than design time. But that's no big deal and could be picked up with utilities like <a href="http://en.wikipedia.org/wiki/FxCop" rel="nofollow noreferrer">FxCop</a> (which we don't use at work).</p>
  <p>Special thanks to Jon Limjap on this one!</p>