MethodBuilder.MakeGenericMethod(Type[]) 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.
Az aktuális általános metódusdefinícióból létrehozott általános metódust ad vissza a megadott általános típusargumentumok használatával.
public:
override System::Reflection::MethodInfo ^ MakeGenericMethod(... cli::array <Type ^> ^ typeArguments);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")>]
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")>]
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
Public Overrides Function MakeGenericMethod (ParamArray typeArguments As Type()) As MethodInfo
Paraméterek
Válaszok
Az MethodInfo aktuális általános metódusdefinícióból a megadott általános típusargumentumok használatával létrehozott általános metódust jelöli.
- Attribútumok
Példák
Az alábbi példakód egy nem teljes általános metódusdefinícióból létrehoz egy létrehozott metódust hiányos típusban.
A példa létrehoz egy átmeneti szerelvényt és modult egyetlen típussal, hozzáad egy metódust M, és általánossá teszi a metódust egy T típusparaméter hozzáadásával a DefineGenericParameters metódus használatával. A típusparaméter a metódus paraméterének típusaként és visszatérési típusaként is használatos. Az általános metódusdefiníció nem kap törzset, és a beágyazási típus nem fejeződik be. Ezután a MakeGenericMethod metódust használja a M<String> (M(Of String) Visual Basic). A példakódnak nincs kimenete, mert a metódus által visszaadott alosztály MethodInfo nem teszi lehetővé a MakeGenericMethod paraméterek tükrözését.
Note
Egy másik példakódra, amely a következőt használjaMakeGenericMethod:DefineGenericParameters MakeGenericMethod az általános típusokat használó kódok kibocsátásakor is széles körben használatos. Lásd : Általános metódus definiálása a Reflection Emit használatával.
using System;
using System.Reflection;
using System.Reflection.Emit;
class Example
{
public static void Main()
{
// Define a transient dynamic assembly (only to run, not
// to save) with one module and a type "Test".
//
AssemblyName aName = new AssemblyName("MyDynamic");
AssemblyBuilder ab =
AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
TypeBuilder tb = mb.DefineType("Test");
// Add a public static method "M" to Test, and make it a
// generic method with one type parameter named "T").
//
MethodBuilder meb = tb.DefineMethod("M",
MethodAttributes.Public | MethodAttributes.Static);
GenericTypeParameterBuilder[] typeParams =
meb.DefineGenericParameters(new string[] { "T" });
// Give the method one parameter, of type T, and a
// return type of T.
meb.SetParameters(typeParams);
meb.SetReturnType(typeParams[0]);
// Create a MethodInfo for M<string>, which can be used in
// emitted code. This is possible even though the method
// does not yet have a body, and the enclosing type is not
// created.
MethodInfo mi = meb.MakeGenericMethod(typeof(string));
// Note that this is actually a subclass of MethodInfo,
// which has rather limited capabilities -- for
// example, you cannot reflect on its parameters.
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Class Example
Public Shared Sub Main()
' Define a transient dynamic assembly (only to run, not
' to save) with one module and a type "Test".
'
Dim aName As AssemblyName = New AssemblyName("MyDynamic")
Dim ab As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
aName, _
AssemblyBuilderAccess.Run)
Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name)
Dim tb As TypeBuilder = mb.DefineType("Test")
' Add a Public Shared method "M" to Test, and make it a
' generic method with one type parameter named "T").
'
Dim meb As MethodBuilder = tb.DefineMethod("M", _
MethodAttributes.Public Or MethodAttributes.Static)
Dim typeParams() As GenericTypeParameterBuilder = _
meb.DefineGenericParameters(New String() { "T" })
' Give the method one parameter, of type T, and a
' return type of T.
meb.SetParameters(typeParams)
meb.SetReturnType(typeParams(0))
' Create a MethodInfo for M(Of String), which can be used
' in emitted code. This is possible even though the method
' does not yet have a body, and the enclosing type is not
' created.
Dim mi As MethodInfo = _
meb.MakeGenericMethod(GetType(String))
' Note that this is actually a subclass of MethodInfo,
' which has rather limited capabilities -- for
' example, you cannot reflect on its parameters.
End Sub
End Class
Megjegyzések
Dinamikus kódkibocsátásakor előfordulhat, hogy a beágyazási típus befejeződése előtt ki kell adnia egy hívást egy, az egy MethodBuilderáltal képviselt általános metódusdefinícióból létrehozott metódusra. A metódussal MakeGenericMethod létrehozhat egy MethodInfo ilyen létrehozott metódust, és használhatja a MethodInfo kibocsátott hívást.