MethodBuilder.MakeGenericMethod(Type[]) Metodo

Definizione

Restituisce un metodo generico costruito dalla definizione di metodo generico corrente usando gli argomenti 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 oggetti Type che rappresentano gli argomenti tipo per il metodo generico.

Restituisce

Oggetto MethodInfo che rappresenta un metodo generico costruito dalla definizione di metodo generico corrente usando gli argomenti di tipo generico specificati.

Esempio

Nell'esempio di codice seguente viene creato un metodo costruito da una definizione di metodo generica incompleta in un tipo incompleto.

L'esempio crea un assembly e un modulo temporaneo con un singolo tipo, aggiunge un metodo e rende il metodo generico aggiungendo un parametro di tipo T usando il DefineGenericParameters metodo M. Il parametro di tipo viene usato come tipo del parametro del metodo e anche come tipo restituito. La definizione del metodo generico non viene assegnata a un corpo e il tipo di racchiuso non viene completato. Il MakeGenericMethod metodo viene quindi usato per creare il metodo M<String> costruito (M(Of String) in Visual Basic). Il codice di esempio non ha alcun output, perché la sottoclasse restituita MethodInfo dal MakeGenericMethod metodo non consente la reflection sui relativi parametri.

Nota

Per un altro esempio di codice che usa MakeGenericMethod, vedere DefineGenericParameters. MakeGenericMethod viene usato anche ampiamente durante l'emissione di codice che usa tipi generici. Vedere Procedura: Definire un metodo generico con Reflection Emit.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

void main()
{
    // Define a transient dynamic assembly (only to run, not
    // to save) with one module and a type "Test".
    // 
    AssemblyName^ aName = gcnew 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);
    array<GenericTypeParameterBuilder^>^ typeParams = 
        meb->DefineGenericParameters(gcnew array<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(String::typeid);
    // Note that this is actually a subclass of MethodInfo, 
    // which has rather limited capabilities -- for
    // example, you cannot reflect on its parameters.
}
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 emette codice dinamico, potrebbe essere necessario generare una chiamata a un metodo costruito dalla definizione del metodo generico rappresentata da un MethodBuilder, prima che il tipo di racchiuso sia stato completato. È possibile usare il MakeGenericMethod metodo per creare un MethodInfo metodo per tale metodo costruito e usare l'oggetto MethodInfo nella chiamata generata.

Si applica a

Vedi anche