MethodBuilder.MakeGenericMethod(Type[]) 方法

定义

返回使用指定泛型类型参数从当前泛型方法定义构造的泛型方法。

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

参数

typeArguments
Type[]

表示泛型方法的类型参数的 Type 对象的数组。

返回

一个 MethodInfo,它表示使用指定泛型类型参数从当前泛型方法定义构造的泛型方法。

示例

下面的代码示例从不完整类型中的不完整泛型方法定义创建构造的方法。

该示例使用单个类型创建暂时性程序集和模块,添加方法 M,并使用 方法添加类型参数 T DefineGenericParameters ,使方法成为泛型。 类型参数用作方法参数的类型,也用作其返回类型。 未为泛型方法定义提供正文,并且封闭类型未完成。 MakeGenericMethod然后使用 方法在 Visual Basic) M(Of String) 中 (构造的方法M<String>。 示例代码没有输出,因为 方法返回的 MethodInfoMakeGenericMethod 子类不允许对其参数进行反射。

注意

有关使用 MakeGenericMethod的另一个代码示例,请参阅 DefineGenericParametersMakeGenericMethod 在发出使用泛型类型的代码时,也会广泛使用 。 请参阅 如何:使用反射发出定义泛型方法

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

注解

发出动态代码时,可能需要在完成封闭类型之前发出对由 表示 MethodBuilder的泛型方法定义构造的方法的调用。 可以使用 MakeGenericMethod 方法为此类构造方法创建 , MethodInfo 并在 MethodInfo 发出的调用中使用 。

适用于

另请参阅