Type.GetGenericTypeDefinition Method
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| The current type is not a generic type. That is, IsGenericType returns false. | |
| The invoked method is not supported in the base class. Derived classes must provide an implementation. |
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: |
|---|
| 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.
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 type; the constructed type represents a Dictionary of Test objects with string keys.
using namespace System; using namespace System::Reflection; using namespace System::Collections::Generic; public ref class Test { public: static void Main() { Console::Write( L"\r\n--- Get the generic type that " ); Console::WriteLine( L"defines a constructed type." ); // Create a Dictionary of Test objects, using strings for the // keys. Dictionary< String^,Test^ >^ d = gcnew Dictionary< String^,Test^ >; // Get a Type object representing the constructed type. // Type^ constructed = d->GetType(); DisplayTypeInfo( constructed ); Type^ myGeneric = constructed->GetGenericTypeDefinition(); DisplayTypeInfo( myGeneric ); } private: static void DisplayTypeInfo( Type^ t ) { Console::WriteLine( L"\r\n{0}", t ); Console::WriteLine( L"\tIs this a generic type definition? {0}", t->IsGenericTypeDefinition ); Console::WriteLine( L"\tIs it a generic type? {0}", t->IsGenericType ); 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); Console::WriteLine( L"\t\t{0}", tParam ); } } }; int main() { Test::Main(); } /* 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 */
import System.*;
import System.Reflection.*;
import System.Collections.Generic.*;
public class Test
{
public static void main(String[] args)
{
Console.WriteLine("\r\n--- Get the generic type that defines "
+ "a constructed type.");
// Create a Dictionary of Test objects, using Strings for the
// keys.
Dictionary<String,Test> d = new Dictionary<String,Test>();
// Get a Type object representing the constructed type.
//
Type constructed = d.GetType();
DisplayTypeInfo(constructed);
Type generic = constructed.GetGenericTypeDefinition();
DisplayTypeInfo(generic);
} //main
private static void DisplayTypeInfo(Type t)
{
Console.WriteLine("\r\n{0}", t);
Console.WriteLine("\tIs this a generic type definition? {0}",
(System.Boolean)t.get_IsGenericTypeDefinition());
Console.WriteLine("\tIs it a generic type? {0}",
(System.Boolean)t.get_IsGenericType());
Type typeArguments[] = t.GetGenericArguments();
Console.WriteLine("\tList type arguments ({0}):",
(Int32)typeArguments.get_Length());
for (int iCtr = 0; iCtr < typeArguments.get_Length(); iCtr++) {
Type tParam = (Type)typeArguments.get_Item(iCtr);
Console.WriteLine("\t\t{0}", tParam);
}
} //DisplayTypeInfo
} //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
*/
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Important: