共用方式為


Type.GetGenericTypeDefinition Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a Type object that represents a generic type definition from which the current generic type can be constructed.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function GetGenericTypeDefinition As Type
public virtual Type GetGenericTypeDefinition()

Return Value

Type: System.Type
A Type object representing a generic type from which the current type can be constructed.

Exceptions

Exception Condition
InvalidOperationException

The current type is not a generic type. That is, IsGenericType returns false.

NotSupportedException

The invoked method is not supported in the base class. Derived classes must provide an implementation.

Remarks

A generic type definition is a template from which other types can be constructed. For example, from the generic type definition G<T> (expressed in C# syntax; G(Of T) in Visual Basic or generic <typename T> ref class G in C++) you can construct and instantiate the type G<int> (G(Of Integer) in Visual Basic). Given a Type object representing this constructed type, the GetGenericTypeDefinition method returns the generic type definition.

If two constructed types are created from the same generic type definition, using the same type arguments, the GetGenericTypeDefinition method returns the same Type object for both types.

If you call the GetGenericTypeDefinition method on a Type object that already represents a generic type definition, it returns the current Type.

Important noteImportant Note:

An array of generic types is not itself generic. In the C# code A<int>[] v; or the Visual Basic code Dim v() As A(Of Integer), the type of variable v is not generic. Use IsGenericType to determine whether a type is generic before calling GetGenericTypeDefinition.

For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

Examples

The following code example creates an instance of a constructed type by using ordinary instance creation and then uses the GetType and GetGenericTypeDefinition methods to retrieve the constructed type and the generic type definition. This example uses the generic Dictionary<TKey, TValue> type; the constructed type represents a Dictionary<TKey, TValue> of Test objects with string keys.

Imports System.Reflection
Imports System.Collections.Generic


Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= vbCrLf & "--- Get the generic type that defines a constructed type." & vbCrLf

      ' Create a Dictionary of Example objects, using strings for the
      ' keys.
      Dim d As New Dictionary(Of String, Example)

      ' Get a Type object representing the constructed type.
      '
      Dim constructed As Type = d.GetType()
      DisplayTypeInfo(outputBlock, constructed)

      Dim generic As Type = constructed.GetGenericTypeDefinition()
      DisplayTypeInfo(outputBlock, generic)
   End Sub 'Main

   Private Shared Sub DisplayTypeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal t As Type)
      outputBlock.Text &= vbCrLf & t.ToString() & vbCrLf
      outputBlock.Text &= vbTab & "Is this a generic type definition? " _
          & t.IsGenericTypeDefinition & vbCrLf
      outputBlock.Text &= vbTab & "Is it a generic type? " _
          & t.IsGenericType & vbCrLf
      Dim typeArguments As Type() = t.GetGenericArguments()
      outputBlock.Text &= vbTab & "List type arguments (" _
          & typeArguments.Length & "):" & vbCrLf
      For Each tParam As Type In typeArguments
         outputBlock.Text &= vbTab & vbTab & tParam.ToString() & vbCrLf
      Next tParam
   End Sub 'DisplayTypeInfo
End Class 'Test

' This example produces the following output:
'
'--- Get the generic type that defines a constructed type.
'
'System.Collections.Generic.Dictionary`2[System.String,Test]
'        Is this a generic type definition? False
'        Is it a generic type? True
'        List type arguments (2):
'                System.String
'                Test
'
'System.Collections.Generic.Dictionary`2[TKey,TValue]
'        Is this a generic type definition? True
'        Is it a generic type? True
'        List type arguments (2):
'                TKey
'                TValue
' 
using System;
using System.Reflection;
using System.Collections.Generic;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "\r\n--- Get the generic type that defines a constructed type." + "\n";

      // Create a Dictionary of Example objects, using strings for the
      // keys.       
      Dictionary<string, Example> d = new Dictionary<string, Example>();

      // Get a Type object representing the constructed type.
      //
      Type constructed = d.GetType();
      DisplayTypeInfo(outputBlock, constructed);

      Type generic = constructed.GetGenericTypeDefinition();
      DisplayTypeInfo(outputBlock, generic);
   }

   private static void DisplayTypeInfo(System.Windows.Controls.TextBlock outputBlock, Type t)
   {
      outputBlock.Text += String.Format("\r\n{0}", t) + "\n";
      outputBlock.Text += String.Format("\tIs this a generic type definition? {0}",
          t.IsGenericTypeDefinition) + "\n";
      outputBlock.Text += String.Format("\tIs it a generic type? {0}",
          t.IsGenericType) + "\n";
      Type[] typeArguments = t.GetGenericArguments();
      outputBlock.Text += String.Format("\tList type arguments ({0}):", typeArguments.Length) + "\n";
      foreach (Type tParam in typeArguments)
      {
         outputBlock.Text += String.Format("\t\t{0}", tParam) + "\n";
      }
   }
}

/* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.