M_Cyjb_TypeUtil_IsExplicitFrom - CYJB/Cyjb GitHub Wiki
确定当前 Type 的实例是否可以从 fromType 的实例显式类型转换得到。
Namespace: Cyjb
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6
C#
public static bool IsExplicitFrom(
this Type type,
Type fromType
)
- type
- Type: System.Type
当前类型。 - fromType
- Type: System.Type
要判断能否显式类型转换得到的类型。
Type: Boolean
如果当前 Type 的实例是否可以从 fromType 的实例显式类型转换得到,则为 true
;否则为 false
。
在 Visual Basic 和 C# 中,这个方法可以当成为类型Type的实例方法来调用。在采用实例方法语法调用这个方法时,请省略第一个参数。请参考 扩展方法 (Visual Basic) 或 扩展方法 (C# 编程指南) 获取更多信息。
异常 | 条件 |
---|---|
ArgumentNullException |
type 为 null 。 |
ArgumentNullException |
fromType 为 null 。 |
判断类型间能否进行显式类型转换的算法来自于 《CSharp Language Specification》v5.0 的第 6.1 节。
下面是 IsExplicitFrom(Type, Type) 方法的一些实例。
Console.WriteLine(typeof(uint).IsExplicitFrom(typeof(object))); // True
Console.WriteLine(typeof(short).IsExplicitFrom(typeof(int))); // True
Console.WriteLine(typeof(int).IsExplicitFrom(typeof(long?))); // True