Skip to content

Instantly share code, notes, and snippets.

@hazzik
Created May 28, 2011 17:51
Show Gist options
  • Save hazzik/997069 to your computer and use it in GitHub Desktop.
Save hazzik/997069 to your computer and use it in GitHub Desktop.
bool CanConvert(Type source, Type destination)
{
try
{
Expression.Convert(Expression.Default(source), destination);
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
@AlexMAS
Copy link

AlexMAS commented May 30, 2011

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