Created
May 28, 2011 17:51
-
-
Save hazzik/997069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool CanConvert(Type source, Type destination) | |
{ | |
try | |
{ | |
Expression.Convert(Expression.Default(source), destination); | |
return true; | |
} | |
catch (InvalidOperationException) | |
{ | |
return false; | |
} | |
} |
Чисто теоретически - да;) Но, мне придется расковыривать исходники Expression.Convert, чтоб убрать даункасты. проблема в том, что эти методы внутренние, и по другому никак нельзя проверить, не написав тонны кода:(
Согласен, задача не тривиальная. Если это решение задачу решает, почему бы и нет ;)
bool CanCast(Type s, Type d)
{
if (!d.IsAssignableFrom(s) && s.IsAssignableFrom(d))
{
return false;
}
try
{
Expression.Convert(Expression.Default(s), d);
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Такое поведение нормально?