title: Test if Convert.ChangeType will work between two types
body: <p>This is a follow-up to <a href="https://stackoverflow.com/questions/1398796/casting-with-reflection">this question</a> about converting values with reflection. Converting an object of a certain type to another type can be done like this:</p>
  
  <pre><code>object convertedValue = Convert.ChangeType(value, targetType);
  </code></pre>
  
  <p>Given two Type instances (say FromType and ToType), is there a way to test whether the conversion will succeed? </p>
  
  <p>E.g. can I write an extension method like this:</p>
  
  <pre><code>public static class TypeExtensions
  {
      public static bool CanChangeType(this Type fromType, Type toType)
      {
          // what to put here?
      }
  }
  </code></pre>
  
  <p>EDIT: This is what I have right now. Ugly, but I don't see another way yet...</p>
  
  <pre><code>bool CanChangeType(Type sourceType, Type targetType)
  {
    try
    {
      var instanceOfSourceType = Activator.CreateInstance(sourceType);
      Convert.ChangeType(instanceOfSourceType, targetType);
      return true; // OK, it can be converted
    }
    catch (Exception ex)
    {
      return false;
    }
  </code></pre>