Type.GetGenericTypeDefinition Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Olyan objektumot Type ad vissza, amely egy általános típusdefiníciót jelöl, amelyből az aktuális általános típus felépíthető.
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
Válaszok
Egy Type általános típust képviselő objektum, amelyből az aktuális típus felépíthető.
Kivételek
Az aktuális típus nem általános típus. Vagyis IsGenericType visszaadja false.
A meghívott metódus nem támogatott az alaposztályban. A származtatott osztályoknak implementációt kell biztosítaniuk.
Példák
Az alábbi példakód egy konstruktált típus egy példányát hozza létre a szokásos példánylétrehozással, majd a létrehozott típus és GetType az általános típusdefiníció lekéréséhez használja a GetGenericTypeDefinition metódusokat és a metódusokat. Ez a példa az általános Dictionary<TKey,TValue> típust használja; a létrehozott típus sztringkulcsokkal Dictionary<TKey,TValue>Test rendelkező objektumokat jelöl.
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
*/
open System
open System.Collections.Generic
type Test() = class end
let displayTypeInfo (t: Type) =
printfn $"\n{t}"
printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
printfn $"\tIs it a generic type? {t.IsGenericType}"
let typeArguments = t.GetGenericArguments()
printfn $"\tList type arguments ({typeArguments.Length}):"
for tParam in typeArguments do
printfn $"\t\t{tParam}"
printfn "\n--- Get the generic type that defines a constructed type."
// Create a Dictionary of Test objects, using strings for the keys.
let d = Dictionary<string, Test>()
// Get a Type object representing the constructed type.
let constructed = d.GetType()
displayTypeInfo constructed
let generic = constructed.GetGenericTypeDefinition()
displayTypeInfo generic
(* 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
'
Megjegyzések
Az általános típusdefiníciók olyan sablonok, amelyekből más típusok is létrehozhatók. A C# általános típusdefiníciójából G<T>például létrehozhatja és példányosíthatja a típust G<int>. Az ilyen létrehozott típust Type képviselő objektum esetében a GetGenericTypeDefinition metódus az általános típusdefiníciót adja vissza.
Ha két építésű típus jön létre ugyanabból az általános típusdefinícióból, ugyanazokkal a típusargumentumokkal, a GetGenericTypeDefinition metódus ugyanazt Type az objektumot adja vissza mindkét típushoz.
Ha egy olyan objektum metódusát GetGenericTypeDefinitionType hívja meg, amely már egy általános típusdefiníciót jelöl, az aktuális Typeértéket adja vissza.
Important
Az általános típusok tömbje önmagában nem általános. A C#-kódban A<int>[] v; vagy a Visual Basic kód Dim v() As A(Of Integer), a v változó típusa nem általános. A hívás IsGenericTypeelőtt annak meghatározására használhatóGetGenericTypeDefinition, hogy egy típus általános-e.
Az általános tükrözésben használt kifejezések invariáns feltételeinek listáját a IsGenericType tulajdonság megjegyzéseiben találja.