M_Cyjb_TypeUtil_IsImplicitFrom - CYJB/Cyjb GitHub Wiki

TypeUtil.IsImplicitFrom 方法

确定当前 Type 的实例是否可以从 fromType 的实例隐式类型转换得到。

Namespace: Cyjb
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6

语法

C#

public static bool IsImplicitFrom(
	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# 编程指南) 获取更多信息。

Exceptions

 

异常 条件
ArgumentNullException typenull
ArgumentNullException fromTypenull

Remarks

判断类型间能否进行隐式类型转换的算法来自于 《CSharp Language Specification》v5.0 的第 6.1 节。

示例

下面是 IsImplicitFrom(Type, Type) 方法与 IsAssignableFrom(Type) 方法的一些对比。

Console.WriteLine(typeof(object).IsAssignableFrom(typeof(uint))); // True 
Console.WriteLine(typeof(object).IsImplicitFrom(typeof(uint))); // True
Console.WriteLine(typeof(int).IsAssignableFrom(typeof(short))); // False
Console.WriteLine(typeof(int).IsImplicitFrom(typeof(short))); // True
Console.WriteLine(typeof(long?).IsAssignableFrom(typeof(int?))); // False
Console.WriteLine(typeof(long?).IsImplicitFrom(typeof(int?))); // True

参见

Reference

TypeUtil 类
Cyjb 命名空间

Other Resources

《C# 判断类型间能否隐式或强制类型转换,以及开放泛型类型转换》
⚠️ **GitHub.com Fallback** ⚠️