Type.IsGenericParameter 屬性

定義

取得值,指出目前的 Type 是否表示泛型類型或泛型方法定義中的類型參數。Gets a value indicating whether the current Type represents a type parameter in the definition of a generic type or method.

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

屬性值

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 Type 代表泛型型別定義之物件的方法,或 GetGenericArguments 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 屬性會 true 針對所產生陣列的每個元素傳回。For a generic type or method definition, the IsGenericParameter property returns true for every element of the resulting array.

  • 針對封閉的結構化類型或方法,此 IsGenericParameter 屬性會 false 針對方法所傳回之陣列的每個元素傳回 GetGenericArgumentsFor 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. IsGenericParameter 傳回 false 類型和 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.

適用於

另請參閱