泛型和反射(C# 编程指南)Generics and Reflection (C# Programming Guide)

因为公共语言运行时 (CLR) 能够在运行时访问泛型类型信息,所以可以使用反射获取关于泛型类型的信息,方法与用于非泛型类型的方法相同。Because the Common Language Runtime (CLR) has access to generic type information at run time, you can use reflection to obtain information about generic types in the same way as for non-generic types. 有关详细信息,请参阅运行时中的泛型For more information, see Generics in the Run Time.

在 .NET Framework 2.0 中,向 Type 类添加了多个新成员来启用泛型类型的运行时信息。In the .NET Framework 2.0, several new members are added to the Type class to enable run-time information for generic types. 有关如何使用这些方法和属性的详细信息,请参阅这些类的文档。See the documentation on these classes for more information on how to use these methods and properties. System.Reflection.Emit 命名空间还包含支持泛型的新成员。The System.Reflection.Emit namespace also contains new members that support generics. 请参阅操作说明:使用反射发出定义泛型类型See How to: Define a Generic Type with Reflection Emit.

有关泛型反射中使用的术语的固定条件列表,请参阅 IsGenericType 属性注解。For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

System.Type 成员名称System.Type Member Name 说明Description
IsGenericType 如果类型是泛型,则返回 true。Returns true if a type is generic.
GetGenericArguments 返回 Type 对象的数组,这些对象表示为构造类型提供的类型实参或泛型类型定义的类型形参。Returns an array of Type objects that represent the type arguments supplied for a constructed type, or the type parameters of a generic type definition.
GetGenericTypeDefinition 返回当前构造类型的基础泛型类型定义。Returns the underlying generic type definition for the current constructed type.
GetGenericParameterConstraints 返回表示当前泛型类型参数约束的 Type 对象的数组。Returns an array of Type objects that represent the constraints on the current generic type parameter.
ContainsGenericParameters 如果类型或任何其封闭类型或方法包含未提供特定类型的类型参数,则返回 true。Returns true if the type or any of its enclosing types or methods contain type parameters for which specific types have not been supplied.
GenericParameterAttributes 获取描述当前泛型类型参数的特殊约束的 GenericParameterAttributes 标志组合。Gets a combination of GenericParameterAttributes flags that describe the special constraints of the current generic type parameter.
GenericParameterPosition 对于表示类型参数的 Type 对象,获取类型参数在声明其类型参数的泛型类型定义或泛型方法定义的类型参数列表中的位置。For a Type object that represents a type parameter, gets the position of the type parameter in the type parameter list of the generic type definition or generic method definition that declared the type parameter.
IsGenericParameter 获取一个值,该值指示当前 Type 是否表示泛型类型或方法定义中的类型参数。Gets a value that indicates whether the current Type represents a type parameter of a generic type or method definition.
IsGenericTypeDefinition 获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。Gets a value that indicates whether the current Type represents a generic type definition, from which other generic types can be constructed. 如果该类型表示泛型类型的定义,则返回 true。Returns true if the type represents the definition of a generic type.
DeclaringMethod 返回定义当前泛型类型参数的泛型方法,如果类型参数未由泛型方法定义,则返回 null。Returns the generic method that defined the current generic type parameter, or null if the type parameter was not defined by a generic method.
MakeGenericType 替代由当前泛型类型定义的类型参数组成的类型数组的元素,并返回表示结果构造类型的 Type 对象。Substitutes the elements of an array of types for the type parameters of the current generic type definition, and returns a Type object representing the resulting constructed type.

此外,MethodInfo 类的成员还为泛型方法启用运行时信息。In addition, members of the MethodInfo class enable run-time information for generic methods. 有关用于反射泛型方法的术语的固定条件列表,请参阅 IsGenericMethod 属性注解。See the IsGenericMethod property remarks for a list of invariant conditions for terms used to reflect on generic methods.

System.Reflection.MemberInfo 成员名称System.Reflection.MemberInfo Member Name 说明Description
IsGenericMethod 如果方法是泛型,则返回 true。Returns true if a method is generic.
GetGenericArguments 返回类型对象的数组,这些对象表示构造泛型方法的类型实参或泛型方法定义的类型形参。Returns an array of Type objects that represent the type arguments of a constructed generic method or the type parameters of a generic method definition.
GetGenericMethodDefinition 返回当前构造方法的基础泛型方法定义。Returns the underlying generic method definition for the current constructed method.
ContainsGenericParameters 如果方法或任何其封闭类型包含未提供特定类型的任何类型参数,则返回 true。Returns true if the method or any of its enclosing types contain any type parameters for which specific types have not been supplied.
IsGenericMethodDefinition 如果当前 MethodInfo 表示泛型方法的定义,则返回 true。Returns true if the current MethodInfo represents the definition of a generic method.
MakeGenericMethod 用类型数组的元素替代当前泛型方法定义的类型参数,并返回表示结果构造方法的 MethodInfo 对象。Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method.

请参阅See also