Type.IsGenericParameter Type.IsGenericParameter Type.IsGenericParameter Type.IsGenericParameter Property

定义

获取一个值,该值指示当前 Type 是否表示泛型类型或方法的定义中的类型参数。Gets a value indicating whether the current Type represents a type parameter in the definition of a generic type or method.

public:
 virtual property bool IsGenericParameter { bool get(); };
public virtual bool IsGenericParameter { get; }
member this.IsGenericParameter : bool
Public Overridable ReadOnly Property IsGenericParameter As Boolean

属性值

如果 Type 对象表示泛型类型定义或泛型方法定义的类型参数,则为 true;否则为 falsetrue if the Type object represents a type parameter of a generic type definition or generic method definition; otherwise, false.

示例

下面的示例使用IsGenericParameter属性测试泛型类型中的泛型类型参数。The following example uses the IsGenericParameter property to test for generic type parameters in a generic type.

if ( t->IsGenericType )
{
   
   // If this is a generic type, display the type arguments.
   //
   array<Type^>^typeArguments = t->GetGenericArguments();
   Console::WriteLine( L"\tList type arguments ({0}):",
      typeArguments->Length );
   System::Collections::IEnumerator^ myEnum =
      typeArguments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Type^ tParam = safe_cast<Type^>(myEnum->Current);
      
      // If this is a type parameter, display its
      // position.
      //
      if ( tParam->IsGenericParameter )
      {
         Console::WriteLine(
            L"\t\t{0}\t(unassigned - parameter position {1})",
            tParam, tParam->GenericParameterPosition );
      }
      else
      {
         Console::WriteLine( L"\t\t{0}", tParam );
      }
   }
}
if (t.IsGenericType)
{
    // If this is a generic type, display the type arguments.
    //
    Type[] typeArguments = t.GetGenericArguments();

    Console.WriteLine("\tList type arguments ({0}):", 
        typeArguments.Length);

    foreach (Type tParam in typeArguments)
    {
        // If this is a type parameter, display its
        // position.
        //
        if (tParam.IsGenericParameter)
        {
            Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                tParam,
                tParam.GenericParameterPosition);
        }
        else
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}
If t.IsGenericType Then
    ' If this is a generic type, display the type arguments.
    '
    Dim typeArguments As Type() = t.GetGenericArguments()
    
    Console.WriteLine(vbTab & "List type arguments (" _
        & typeArguments.Length & "):")
    
    For Each tParam As Type In typeArguments
        ' If this is a type parameter, display its position.
        '
        If tParam.IsGenericParameter Then
            Console.WriteLine(vbTab & vbTab & tParam.ToString() _
                & vbTab & "(unassigned - parameter position " _
                & tParam.GenericParameterPosition & ")")
        Else
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        End If
    Next tParam
End If

注解

Type可以通过调用GetGenericArguments表示GetGenericArguments泛型类型定义的Type对象的方法MethodInfo或表示泛型方法的对象的方法来获取表示泛型类型参数的对象。定义.Type objects that represent generic type parameters can be obtained by calling the GetGenericArguments method of a Type object that represents a generic type definition, or the GetGenericArguments method of a MethodInfo object that represents a generic method definition.

  • 对于泛型类型或方法定义, IsGenericParameter属性为所得数组的每个元素返回。 trueFor a generic type or method definition, the IsGenericParameter property returns true for every element of the resulting array.

  • 对于封闭式构造类型或方法, IsGenericParameter属性为该GetGenericArguments方法false返回的数组的每个元素返回。For a closed constructed type or method, the IsGenericParameter property returns false for every element of the array returned by the GetGenericArguments method.

  • 对于开放构造的类型或方法,数组的某些元素可能是特定类型,而其他元素可能为类型参数。For an open constructed type or method, some elements of the array might be specific types and others might be type parameters. IsGenericParameterfalse类型参数和true类型参数返回。IsGenericParameter returns false for the types and true for the type parameters. ContainsGenericParameters属性的代码示例演示了混合类型和类型参数的泛型类。The code example for the ContainsGenericParameters property demonstrates a generic class with a mixture of types and type parameters.

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

适用于

另请参阅