MethodBuilder.MakeGenericMethod(Type[]) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce un metodo generico costruito dalla definizione del metodo generico corrente utilizzando gli argomenti di tipo generico specificati.
public:
override System::Reflection::MethodInfo ^ MakeGenericMethod(... cli::array <Type ^> ^ typeArguments);
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
Public Overrides Function MakeGenericMethod (ParamArray typeArguments As Type()) As MethodInfo
Parametri
- typeArguments
- Type[]
Matrice di Type oggetti che rappresentano gli argomenti di tipo per il metodo generico.
Valori restituiti
Oggetto MethodInfo che rappresenta il metodo generico costruito dalla definizione del metodo generico corrente utilizzando gli argomenti di tipo generico specificati.
Esempio
Nell'esempio di codice seguente viene creato un metodo costruito da una definizione di metodo generico incompleta in un tipo incompleto.
L'esempio crea un assembly temporaneo e un modulo con un singolo tipo, aggiunge un metodo e rende il metodo Mgenerico aggiungendo un parametro di tipo T usando il DefineGenericParameters metodo . Il parametro di tipo viene usato come tipo del parametro del metodo e anche come tipo restituito. Alla definizione del metodo generico non viene assegnato un corpo e il tipo di inclusione non viene completato. Il metodo MakeGenericMethod viene quindi usato per rendere il metodo costruito M<String> (M(Of String) in Visual Basic). Il codice di esempio non ha alcun output, perché la sottoclasse di MethodInfo restituita dal metodo non consente la MakeGenericMethod reflection sui relativi parametri.
Annotazioni
Per un altro esempio di codice che usa MakeGenericMethod, vedere DefineGenericParameters. MakeGenericMethod viene usato anche ampiamente durante la creazione di codice che usa tipi generici. Vedere Procedura: Definire un metodo generico con Reflection Emit.
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
Commenti
Quando si genera codice dinamico, potrebbe essere necessario generare una chiamata a un metodo costruito dalla definizione del metodo generico rappresentata da un MethodBuilderoggetto prima del completamento del tipo di inclusione. È possibile utilizzare il MakeGenericMethod metodo per creare un MethodInfo oggetto per un metodo costruito di questo tipo e utilizzare nella MethodInfo chiamata generata.