title: Multiple Type Variable C#
body: <p>I have a bit of a strange issue here.  I have a project constraint where a value of a Property needs to either be a number (<code>int</code>, <code>double</code>, <code>long</code>, etc are all acceptable), a <code>string</code>, or a <code>datetime</code>.  The reason that the Value parameter needs to be of one these three (err..well if you count all of the possible numeric value Types allowed it's a bit more) Types is because depending on the type the underlying value will need to be converted to special formats for serialization to a REST API.  To simplify things here is a basic idea of the class as a POCO:</p>
  
  <pre><code>class Foo
  {
       public string Name {get;set;}
       public Guid Id {get; set;}
       public UNKNOWN Value {get;set;}
  }
  </code></pre>
  
  <p>I thought about using generics for this, with a <code>where T : struct</code> constraint, but this still leaves too many types that can theoretically be set that are actually invalid Types. Sure I can perform type checking and throw  exceptions during construction/setting of the Value parameter, but this doesn't feel like "clean" code.</p>
  
  <p>I took a look at this question <a href="https://stackoverflow.com/questions/3304607/how-do-you-work-with-a-variable-that-can-be-of-multiple-types">How do you work with a variable that can be of multiple types?</a> , but it didn't really help since it was more of an issue dealing with inheritance.  However, using multiple nullable private fields and returning a single Property based on which one was populated is a possibility, but again I feel there has to be a better way.</p>
  
  <p>The other possibility I was thinking of was to use the <code>dynamic</code> type and and perform some reflection magic to check the underlying type (and perform conversions &amp; formatting/throw exceptions).  I'm a bit scared that this will really hurt performance though.  </p>
  
  <p>Are there any best practices for this situation?  If not, are there any better ways to handle this from what I've mentioned?</p>