title: How to check if type can be converted to another type in C#
body: <p>I have two types <code>sourceType</code> and <code>targetType</code> and I need to write a method in C#, which checks if values of <code>sourceType</code> can be assigned to a variable of <code>targetType</code>. The signature of the function is <code>MatchResultTypeAndExpectedType(Type sourceType, Type targetType)</code>.</p>
  
  <p>The inheritance is covered by <a href="http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&amp;l=EN-US&amp;k=k%28System.Type.IsAssignableFrom%29;k%28SolutionItemsProject%29;k%28TargetFrameworkMoniker-.NETFramework" rel="noreferrer">IsAssignableFrom</a>. In the case of convertible types I thought to use <a href="http://msdn.microsoft.com/en-us/library/92580th5.aspx" rel="noreferrer">CanConvertFrom</a>, but, for example, if both types are numerical, then it always returns <code>false</code>.
  A test, which I performed:</p>
  
  <pre><code>TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Decimal));
  Console.WriteLine("Int16 to Decimal - " + typeConverter.CanConvertFrom(typeof(Int16)));
  Console.WriteLine("UInt16 to Decimal - " + typeConverter.CanConvertFrom(typeof(UInt16)));
  
  typeConverter = TypeDescriptor.GetConverter(typeof(long));
  Console.WriteLine("UInt16 to Int64 - " + typeConverter.CanConvertFrom(typeof(uint)));
  
  typeConverter = TypeDescriptor.GetConverter(typeof(Double));
  Console.WriteLine("UInt16 to Double - " + typeConverter.CanConvertFrom(typeof(UInt16)));
  
  typeConverter = TypeDescriptor.GetConverter(typeof(String));
  Console.WriteLine("UInt16 to String - " + typeConverter.CanConvertFrom(typeof(UInt16)));
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>Int16 to Decimal - False
  UInt16 to Decimal - False
  UInt16 to Int64 - False
  UInt16 to Double - False
  UInt16 to String - False
  </code></pre>
  
  <p><strong>[EDIT]</strong> So my question:
  Is there a way in .NET to check whether a value of a given type can be assigned to a variable of another type without knowing values, e.g., whether implicit conversion will succeed? More specific requirements for implementation of <code>MatchResultTypeAndExpectedType(Type sourceType, Type targetType)</code>:</p>
  
  <ol>
  <li>Source and target types are not known at compile time, since their assemblies are loaded later.</li>
  <li>No values or objects can be provided as input.</li>
  <li>No objects of the types can be created in the implementation, since it is not allowed by the rest of the system. Values of value types can be created.</li>
  <li>Only implicit conversion has to be checked.</li>
  </ol>
  
  <p>It is known whether <code>sourceType</code> is value type. So the signature of the method can be like <code>MatchResultTypeAndExpectedType(Type sourceType, Boolean isSourceValueType, Type targetType)</code></p>
  
  <p>One way is to implement <a href="http://msdn.microsoft.com/en-us/library/y5b434w4%28v=vs.110%29.aspx" rel="noreferrer">Implicit Numeric Conversions Table</a>, but it will not cover other conversions or user defined conversions.</p>