MethodBuilder.MakeGenericMethod(Type[]) Método

Definición

Devuelve un método genérico construido a partir de la definición de método genérico actual mediante los argumentos de tipo genérico especificados.

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

Parámetros

typeArguments
Type[]

Matriz de objetos Type que representan los argumentos de tipo del método genérico.

Devoluciones

MethodInfo

MethodInfo que representa el método genérico construido a partir de la definición de método genérico actual mediante los argumentos de tipo genérico especificados.

Ejemplos

En el ejemplo de código siguiente se crea un método construido a partir de una definición de método genérico incompleta en un tipo incompleto.

En el ejemplo se crea un ensamblado y un módulo transitorios con un único tipo, se agrega un método y se convierte el método en genérico mediante la adición de un parámetro M de tipo T mediante el método DefineGenericParameters . El parámetro de tipo se usa como el tipo del parámetro del método y también como su tipo de valor devuelto. La definición del método genérico no tiene un cuerpo y el tipo de contenido no se completa. A MakeGenericMethod continuación, el método se usa para crear el método construido M<String> ( en M(Of String) Visual Basic). El código de ejemplo no tiene ninguna salida, porque la subclase de devuelta por el MethodInfo método no permite la MakeGenericMethod reflexión sobre sus parámetros.

Nota

Para obtener otro ejemplo de código que usa MakeGenericMethod , vea DefineGenericParameters . MakeGenericMethod también se usa ampliamente al emitir código que usa tipos genéricos. Vea Cómo: Definir un método genérico con emisión de reflexión.

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

Comentarios

Al emitir código dinámico, es posible que deba emitir una llamada a un método construido a partir de la definición de método genérico representada por , antes de que se haya completado el tipo MethodBuilder de contenido. Puede usar el método MakeGenericMethod para crear un para este tipo de método construido y usar en la llamada MethodInfo MethodInfo emitida.

Se aplica a

Consulte también