Determine if a reflected type can be cast to another reflected type

前端 未结 5 1149
我在风中等你
我在风中等你 2021-01-21 08:07

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

5条回答
  •  抹茶落季
    2021-01-21 08:47

    To directly answer your question ...

    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)

    ... you can use something similar to this :

    to.GetType().IsAssignableFrom(from.GetType());
    

    The Type.IsAssignableFrom() method can be used for exactly your purpose. This would also be considerably less verbose (even if only marginally more performant) than using TypeConverters.

提交回复
热议问题