In .net (C#), If you have two types discovered through reflection is it possible to determine if one can be cast to the other? (implicit and/or explicit).
What I\'m tryi
public static bool HasConversionOperator( Type from, Type to )
{
Func bodyFunction = body => Expression.Convert( body, to );
ParameterExpression inp = Expression.Parameter( from, "inp" );
try
{
// If this succeeds then we can cast 'from' type to 'to' type using implicit coercion
Expression.Lambda( bodyFunction( inp ), inp ).Compile();
return true;
}
catch( InvalidOperationException )
{
return false;
}
}
This should do the trick for implicit and explicit conversions (including numeric types, classes, etc.)