Type.MakeGenericType(Type[]) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ersetzt die Typparameter der aktuellen generischen Typdefinition durch die Elemente eines Arrays von Typen und gibt ein Type-Objekt zurück, das den resultierenden konstruierten Typ darstellt.
public:
abstract Type ^ MakeGenericType(... cli::array <Type ^> ^ typeArguments);
public:
virtual Type ^ MakeGenericType(... cli::array <Type ^> ^ typeArguments);
public abstract Type MakeGenericType (params Type[] typeArguments);
public virtual Type MakeGenericType (params Type[] typeArguments);
abstract member MakeGenericType : Type[] -> Type
abstract member MakeGenericType : Type[] -> Type
override this.MakeGenericType : Type[] -> Type
Public MustOverride Function MakeGenericType (ParamArray typeArguments As Type()) As Type
Public Overridable Function MakeGenericType (ParamArray typeArguments As Type()) As Type
Parameter
- typeArguments
- Type[]
Ein Array von Typen, die die Typparameter des aktuellen generischen Typs ersetzen sollen.
Gibt zurück
Ein Type, der den konstruierten Typ darstellt, der durch Ersetzen der Typparameter des aktuellen generischen Typs durch die Elemente von typeArguments
erstellt wurde.
Ausnahmen
Der aktuelle Typ stellt keine generische Typdefinition dar. Das heißt, IsGenericTypeDefinition gibt false
zurück.
typeArguments
ist null
.
- oder -
Ein beliebiges Element von typeArguments
ist null
.
Die Anzahl der Elemente im typeArguments
entspricht nicht der Anzahl von Typparametern in der aktuellen generischen Typdefinition.
- oder -
Ein beliebiges Element von typeArguments
erfüllt nicht die Einschränkungen, die für den entsprechenden Typparameter des aktuellen generischen Typs angegeben wurden.
- oder -
typeArguments
enthält ein Element, das ein Zeigertyp (IsPointer gibt true
zurück), vom Typ „by-ref“ (IsByRef gibt true
zurück) oder Void ist.
Die aufgerufene Methode wird in der Basisklasse nicht unterstützt. Abgeleitete Klassen müssen eine Implementation angeben.
Beispiele
Im folgenden Beispiel wird die MakeGenericType -Methode verwendet, um einen konstruierten Typ aus der generischen Typdefinition für den Dictionary<TKey,TValue> Typ zu erstellen. Der konstruierte Typ stellt eine Dictionary<TKey,TValue> von Test
-Objekten mit Zeichenfolgenschlüsseln dar.
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;
namespace Example
{
public ref class Test
{
public:
static void CreateConstructedType(void)
{
Console::WriteLine("\r\n--- Create a constructed type"
" from the generic Dictionary`2 type.");
// Create a type object representing
// the generic Dictionary`2 type.
Type^ genericType = Type::GetType(
"System.Collections.Generic.Dictionary`2");
if (genericType != nullptr)
{
DisplayTypeInfo(genericType);
}
else
{
Console::WriteLine("The type is not found");
return;
}
// Create an array of types to substitute for the type
// parameters of Dictionary`2.
// The key is of type string, and the type to be
// contained in the Dictionary`2 is Test.
array<Type^>^ typeArgs = {String::typeid, Test::typeid};
Type^ constructedType =
genericType->MakeGenericType(typeArgs);
DisplayTypeInfo(constructedType);
// Compare the type objects obtained above to type objects
// obtained using typeof() and GetGenericTypeDefinition().
Console::WriteLine("\r\n--- Compare types obtained by"
" different methods:");
Type^ definedType = Dictionary<String^, Test^>::typeid;
Console::WriteLine("\tAre the constructed types "
"equal? {0}", definedType == constructedType);
Console::WriteLine("\tAre the generic types equal? {0}",
definedType->GetGenericTypeDefinition() == genericType);
}
private:
static void DisplayTypeInfo(Type^ typeToDisplay)
{
Console::WriteLine("\r\n{0}", typeToDisplay);
Console::WriteLine("\tIs this a generic type definition? "
"{0}", typeToDisplay->IsGenericTypeDefinition);
Console::WriteLine("\tIs it a generic type? "
"{0}", typeToDisplay->IsGenericType);
array<Type^>^ typeArguments =
typeToDisplay->GetGenericArguments();
Console::WriteLine("\tList type arguments ({0}):",
typeArguments->Length);
for each (Type^ typeArgument in typeArguments)
{
Console::WriteLine("\t\t{0}", typeArgument);
}
}
};
}
int main(void)
{
Example::Test::CreateConstructedType();
}
/* This example produces the following output:
--- Create a constructed type from the generic Dictionary`2 type.
System.Collections.Generic.Dictionary`2[KeyType,ValueType]
Is this a generic type definition? True
Is it a generic type? True
List type arguments (2):
K
V
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
--- Compare types obtained by different methods:
Are the constructed types equal? True
Are the generic types equal? True
*/
using System;
using System.Reflection;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
Console.WriteLine("\r\n--- Create a constructed type from the generic Dictionary type.");
// Create a type object representing the generic Dictionary
// type, by omitting the type arguments (but keeping the
// comma that separates them, so the compiler can infer the
// number of type parameters).
Type generic = typeof(Dictionary<,>);
DisplayTypeInfo(generic);
// Create an array of types to substitute for the type
// parameters of Dictionary. The key is of type string, and
// the type to be contained in the Dictionary is Test.
Type[] typeArgs = { typeof(string), typeof(Test) };
// Create a Type object representing the constructed generic
// type.
Type constructed = generic.MakeGenericType(typeArgs);
DisplayTypeInfo(constructed);
// Compare the type objects obtained above to type objects
// obtained using typeof() and GetGenericTypeDefinition().
Console.WriteLine("\r\n--- Compare types obtained by different methods:");
Type t = typeof(Dictionary<String, Test>);
Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
Console.WriteLine("\tAre the generic types equal? {0}",
t.GetGenericTypeDefinition() == 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:
--- Create a constructed type from the generic Dictionary type.
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
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
--- Compare types obtained by different methods:
Are the constructed types equal? True
Are the generic types equal? True
*/
open System
open System.Collections.Generic
type Test() = class end
let displayTypeInfo (t: Type) =
printfn $"\r\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 "\r\n--- Create a constructed type from the generic Dictionary type."
// Create a type object representing the generic Dictionary
// type, by calling .GetGenericTypeDefinition().
let generic = typeof<Dictionary<_,_>>.GetGenericTypeDefinition()
displayTypeInfo generic
// Create an array of types to substitute for the type
// parameters of Dictionary. The key is of type string, and
// the type to be contained in the Dictionary is Test.
let typeArgs = [| typeof<string>; typeof<Test> |]
// Create a Type object representing the constructed generic type.
let constructed = generic.MakeGenericType typeArgs
displayTypeInfo constructed
(* This example produces the following output:
--- Create a constructed type from the generic Dictionary type.
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
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
*)
Imports System.Reflection
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Main()
Console.WriteLine(vbCrLf & "--- Create a constructed type from the generic Dictionary type.")
' Create a type object representing the generic Dictionary
' type, by omitting the type arguments (but keeping the
' comma that separates them, so the compiler can infer the
' number of type parameters).
Dim generic As Type = GetType(Dictionary(Of ,))
DisplayTypeInfo(generic)
' Create an array of types to substitute for the type
' parameters of Dictionary. The key is of type string, and
' the type to be contained in the Dictionary is Test.
Dim typeArgs() As Type = { GetType(String), GetType(Test) }
' Create a Type object representing the constructed generic
' type.
Dim constructed As Type = generic.MakeGenericType(typeArgs)
DisplayTypeInfo(constructed)
' Compare the type objects obtained above to type objects
' obtained using GetType() and GetGenericTypeDefinition().
Console.WriteLine(vbCrLf & "--- Compare types obtained by different methods:")
Dim t As Type = GetType(Dictionary(Of String, Test))
Console.WriteLine(vbTab & "Are the constructed types equal? " _
& (t Is constructed))
Console.WriteLine(vbTab & "Are the generic types equal? " _
& (t.GetGenericTypeDefinition() Is 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 ({0}):", _
typeArguments.Length)
For Each tParam As Type In typeArguments
Console.WriteLine(vbTab & vbTab & tParam.ToString())
Next
End Sub
End Class
' This example produces the following output:
'
'--- Create a constructed type from the generic Dictionary type.
'
'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
'
'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
'
'--- Compare types obtained by different methods:
' Are the constructed types equal? True
' Are the generic types equal? True
Hinweise
Weitere Informationen zu dieser API finden Sie unter Ergänzende API-Hinweise zu Type.MakeGenericType.