Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método MethodBuilder.MakeGenericMethod (Type[])

 

Publicado: octubre de 2016

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.

Espacio de nombres:   System.Reflection.Emit
Ensamblado:  mscorlib (en mscorlib.dll)

public override MethodInfo MakeGenericMethod(
	params Type[] typeArguments
)

Parámetros

typeArguments
Type: System.Type[]

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

Valor devuelto

Type: System.Reflection.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.

When you are emitting dynamic code, you might need to emit a call to a method constructed from the generic method definition represented by a T:System.Reflection.Emit.MethodBuilder, before the enclosing type has been completed. You can use the M:System.Reflection.Emit.MethodBuilder.MakeGenericMethod(System.Type[]) method to create a T:System.Reflection.MethodInfo for such a constructed method, and use the T:System.Reflection.MethodInfo in the emitted call.

The following code example creates a constructed method from an incomplete generic method definition in an incomplete type.

The example creates a transient assembly and module with a single type, adds a method M, and makes the method generic by adding a type parameter T using the M:System.Reflection.Emit.MethodBuilder.DefineGenericParameters(System.String[]) method. The type parameter is used as the type of the method's parameter, and also as its return type. The generic method definition is not given a body, and the enclosing type is not completed. The M:System.Reflection.Emit.MethodBuilder.MakeGenericMethod(System.Type[]) method is then used to make the constructed method M<String> (M(Of String) in Visual Basic). The example code has no output, because the subclass of T:System.Reflection.MethodInfo returned by the M:System.Reflection.Emit.MethodBuilder.MakeGenericMethod(System.Type[]) method does not allow reflection over its parameters.

System_CAPS_noteNota

For another code example that uses M:System.Reflection.Emit.MethodBuilder.MakeGenericMethod(System.Type[]), see M:System.Reflection.Emit.MethodBuilder.DefineGenericParameters(System.String[]). M:System.Reflection.Emit.MethodBuilder.MakeGenericMethod(System.Type[]) is also used extensively when emitting code that uses generic types. See How to: Define a Generic Method with 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.
    }
}

.NET Framework
Disponible desde 2.0
Silverlight
Disponible desde 2.0
Volver al principio
Mostrar: