Type.GetGenericArguments 메서드

정의

닫힌 제네릭 형식의 형식 정의나 제네릭 형식 정의의 형식 매개 변수를 나타내는 Type 개체의 배열을 반환합니다.

public:
 virtual cli::array <Type ^> ^ GetGenericArguments();
public virtual Type[] GetGenericArguments ();
abstract member GetGenericArguments : unit -> Type[]
override this.GetGenericArguments : unit -> Type[]
Public Overridable Function GetGenericArguments () As Type()

반환

Type[]

제네릭 형식의 형식 인수를 나타내는 Type 개체의 배열입니다. 현재 형식이 제네릭 형식이 아니면 빈 배열을 반환합니다.

예외

호출된 메서드가 기본 클래스에서 지원되지 않습니다. 파생 클래스에서 구현을 제공해야 합니다.

예제

다음 코드 예제에서는 GetGenericArguments 메서드를 사용하여 생성 형식의 형식 인수와 제네릭 형식 정의의 형식 매개 변수를 표시합니다.

이 코드 예제는에 대해 제공 된 큰 예제의 일부는 IsGenericTypeDefinition 속성입니다. 샘플 출력은 더 큰 예제를 참조하세요.

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

설명

배열 요소는 제네릭 형식의 형식 인수 목록에 나타나는 순서대로 반환됩니다.

  • 현재 형식이 닫힌 생성 형식인 경우(즉, ContainsGenericParameters 속성이 를 false 반환함) 메서드에서 반환하는 GetGenericArguments 배열에는 제네릭 형식 정의의 제네릭 형식 매개 변수에 할당된 형식이 포함됩니다.

  • 현재 형식이 제네릭 형식 정의인 경우 배열에는 형식 매개 변수가 포함됩니다.

  • 현재 형식이 열려 있는 생성 형식인 경우(즉, ContainsGenericParameters 속성이 를 true 반환함) 특정 형식이 묶는 제네릭 형식 또는 메서드의 형식 매개 변수 및 형식 매개 변수에 모두 할당되지 않은 경우 배열에는 형식 및 형식 매개 변수가 모두 포함됩니다. 속성을 사용하여 IsGenericParameter 구분합니다. 이 시나리오의 데모를 보려면 속성에 대한 코드 예제를 ContainsGenericParameters 참조하세요.

제네릭 리플렉션에 사용되는 용어의 고정 조건 목록은 IsGenericType 속성 설명을 참조하세요.

적용 대상

추가 정보