title: How to Convert a String to a Nullable Type Which is Determined at Runtime?
body: <p>I have the below code and I'd need to convert a string to a type which is also specified from String:</p>
  
  <pre><code> Type t = Type.GetType("System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
  
              object d = Convert.ChangeType("2012-02-23 10:00:00", t);
  </code></pre>
  
  <p>I get the below error messagE:</p>
  
  <pre><code>Invalid cast from 'System.String' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
  </code></pre>
  
  <p>How would that be nicely possible?</p>
  
  <p>I know one ugly way would be to check whether the type is nullable using if:</p>
  
  <pre><code>    Type possiblyNullableType = Type.GetType("System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]");
  
      var underlyingType = Nullable.GetUnderlyingType(possiblyNullableType);
  
      Object result;
  
      // if it's null, it means it wasn't nullable
      if (underlyingType != null)
      {
          result = Convert.ChangeType("2012-02-23 10:00:00", underlyingType);
      }
  </code></pre>
  
  <p>Would there be any better way?</p>
  
  <p>Thanks, </p>