Type.GetGenericTypeDefinition 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个表示可用于构造当前泛型类型的泛型类型定义的 Type 对象。
public:
abstract Type ^ GetGenericTypeDefinition();
public:
virtual Type ^ GetGenericTypeDefinition();
public abstract Type GetGenericTypeDefinition ();
public virtual Type GetGenericTypeDefinition ();
abstract member GetGenericTypeDefinition : unit -> Type
abstract member GetGenericTypeDefinition : unit -> Type
override this.GetGenericTypeDefinition : unit -> Type
Public MustOverride Function GetGenericTypeDefinition () As Type
Public Overridable Function GetGenericTypeDefinition () As Type
返回
表示可用于构造当前类型的泛型类型的 Type 对象。
例外
当前类型不是泛型类型。 也就是说,IsGenericType 返回 false
。
基类不支持调用的方法。 派生类必须提供一个实现。
示例
下面的代码示例使用普通实例创建类型的实例,然后使用 和 方法来检索 GetType GetGenericTypeDefinition 构造类型和泛型类型定义。 此示例使用泛型类型 Dictionary<TKey,TValue> ;构造类型表示具有 Dictionary<TKey,TValue> Test
字符串键的对象的 。
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
*/
using System;
using System.Reflection;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
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);
}
private static void DisplayTypeInfo(Type t)
{
Console.WriteLine("\r\n{0}", t);
Console.WriteLine("\tIs this a generic type definition? {0}",
t.IsGenericTypeDefinition);
Console.WriteLine("\tIs it a generic type? {0}",
t.IsGenericType);
Type[] typeArguments = t.GetGenericArguments();
Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
foreach (Type tParam in typeArguments)
{
Console.WriteLine("\t\t{0}", tParam);
}
}
}
/* 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
*/
Imports System.Reflection
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Main()
Console.WriteLine(vbCrLf & "--- Get the generic type that defines a constructed type.")
' Create a Dictionary of Test objects, using strings for the
' keys.
Dim d As New Dictionary(Of String, Test)
' Get a Type object representing the constructed type.
'
Dim constructed As Type = d.GetType()
DisplayTypeInfo(constructed)
Dim generic As Type = constructed.GetGenericTypeDefinition()
DisplayTypeInfo(generic)
End Sub
Private Shared Sub DisplayTypeInfo(ByVal t As Type)
Console.WriteLine(vbCrLf & t.ToString())
Console.WriteLine(vbTab & "Is this a generic type definition? " _
& t.IsGenericTypeDefinition)
Console.WriteLine(vbTab & "Is it a generic type? " _
& t.IsGenericType)
Dim typeArguments As Type() = t.GetGenericArguments()
Console.WriteLine(vbTab & "List type arguments (" _
& typeArguments.Length & "):")
For Each tParam As Type In typeArguments
Console.WriteLine(vbTab & vbTab & tParam.ToString())
Next tParam
End Sub
End Class
' 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
'
注解
泛型类型定义是一个模板,可以从中构造其他类型。 例如,从用 C# 语法 (泛型类型定义;在 Visual Basic 或 C++ 中) 可以在 Visual Basic) 中构造和实例化 (G<T>
G(Of T)
generic <typename T> ref class G
G<int>
G(Of Integer)
类型。 给定 Type 表示此构造类型的 对象,该方法 GetGenericTypeDefinition 返回泛型类型定义。
如果使用相同的类型参数从同一泛型类型定义创建两个构造类型,则该方法将针对这两种类型返回 GetGenericTypeDefinition Type 相同的对象。
如果对已经表示泛型类型定义的 对象调用 方法,它将 GetGenericTypeDefinition Type 返回当前 Type 。
重要
泛型类型的数组本身不是泛型。 在 C# 代码或 A<int>[] v;
Visual Basic Dim v() As A(Of Integer)
代码中,变量的类型 v
不是泛型的。 在 IsGenericType 调用 之前,使用 确定类型是否是泛型 GetGenericTypeDefinition 类型。
有关泛型反射中使用的术语的固定条件列表,请参阅 IsGenericType 属性注解。