M_Cyjb_TypeUtil_CloseDefinitionFrom - CYJB/Cyjb GitHub Wiki
沿着指定 Type 的继承链向上查找,直到找到当前泛型类型定义的封闭构造类型。
Namespace: Cyjb
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6
C#
public static Type? CloseDefinitionFrom(
this Type definition,
Type type
)
- definition
- Type: System.Type
要获取封闭构造类型的泛型类型定义。 - type
- Type: System.Type
要查找继承链的类型。
Type: Type
如果当前的泛型类型定义是 type 继承链中类型的泛型定义, 或者是 type 实现的接口的泛型定义, 或者 type 是泛型类型参数且当前泛型类型定义是 type 的约束之一, 则为相应的封闭构造类型。否则为 null
。
在 Visual Basic 和 C# 中,这个方法可以当成为类型Type的实例方法来调用。在采用实例方法语法调用这个方法时,请省略第一个参数。请参考 扩展方法 (Visual Basic) 或 扩展方法 (C# 编程指南) 获取更多信息。
异常 | 条件 |
---|---|
ArgumentNullException |
definition 为 null 。 |
ArgumentNullException |
type 为 null 。 |
如果 definition 可以找到多个封闭构造类型,则只会返回其中的任意一个。
下面是 CloseDefinitionFrom(Type, Type) 方法的简单示例:
Console.WriteLine(typeof(IEnumerable<>).CloseDefinitionFrom(typeof(List<int>)));
// 输出:System.Collections.Generi.IEnumerable`1[System.Int32]