MethodBuilder.DefineGenericParameters(String[]) 方法

定義

設定目前方法的泛型型別參數數目、指定其名稱,並傳回可用來定義其條件約束的 GenericTypeParameterBuilder 物件陣列。

C#
public System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names);

參數

names
String[]

由字串組成的陣列,代表泛型類型參數的名稱。

傳回

GenericTypeParameterBuilder 物件組成的陣列,代表泛型方法的型別參數。

例外狀況

已定義這個方法的泛型型別參數。

-或-

這個方法已經完成。

-或-

已對目前的執行個體呼叫 SetImplementationFlags(MethodImplAttributes) 方法。

namesnull

-或-

names 的項目是 null

names 是空陣列。

範例

下列程式代碼範例會建立動態類型 , DemoType其中包含動態泛型方法 DemoMethod。 這個方法有兩個泛型型別參數,其中一個做為參數使用,另一個則做為傳回型別。

執行程式碼時,動態組件會儲存為 DemoGenericMethod1.dll,以及可以使用檢查Ildasm.exe (IL 反組譯工具)

備註

此程式代碼範例會產生只傳回 Null 參考的簡單方法主體。 如需使用更完整開發的方法主體來建立和使用泛型類型的程式代碼範例,請參閱 如何:使用反映發出定義泛型方法

C#
using System;
using System.Reflection;
using System.Reflection.Emit;

class DemoMethodBuilder
{

    public static void Main()
    {
        // Creating a dynamic assembly requires an AssemblyName
        // object, and the current application domain.
        //
        AssemblyName asmName =
            new AssemblyName("DemoMethodBuilder1");
        AppDomain domain = AppDomain.CurrentDomain;
        AssemblyBuilder demoAssembly =
            domain.DefineDynamicAssembly(
                asmName,
                AssemblyBuilderAccess.RunAndSave
            );

        // Define the module that contains the code. For an
        // assembly with one module, the module name is the
        // assembly name plus a file extension.
        ModuleBuilder demoModule =
            demoAssembly.DefineDynamicModule(
                asmName.Name,
                asmName.Name + ".dll"
            );

        TypeBuilder demoType = demoModule.DefineType(
            "DemoType",
            TypeAttributes.Public | TypeAttributes.Abstract
        );

        // Define a Shared, Public method with standard calling
        // conventions. Do not specify the parameter types or the
        // return type, because type parameters will be used for
        // those types, and the type parameters have not been
        // defined yet.
        MethodBuilder demoMethod = demoType.DefineMethod(
            "DemoMethod",
            MethodAttributes.Public | MethodAttributes.Static
        );

        // Defining generic parameters for the method makes it a
        // generic method. By convention, type parameters are
        // single alphabetic characters. T and U are used here.
        //
        string[] typeParamNames = {"T", "U"};
        GenericTypeParameterBuilder[] typeParameters =
            demoMethod.DefineGenericParameters(typeParamNames);

        // The second type parameter is constrained to be a
        // reference type.
        typeParameters[1].SetGenericParameterAttributes(
            GenericParameterAttributes.ReferenceTypeConstraint);

        // Use the IsGenericMethod property to find out if a
        // dynamic method is generic, and IsGenericMethodDefinition
        // to find out if it defines a generic method.
        Console.WriteLine("Is DemoMethod generic? {0}",
            demoMethod.IsGenericMethod);
        Console.WriteLine("Is DemoMethod a generic method definition? {0}",
            demoMethod.IsGenericMethodDefinition);

        // Set parameter types for the method. The method takes
        // one parameter, and its type is specified by the first
        // type parameter, T.
        Type[] parms = {typeParameters[0]};
        demoMethod.SetParameters(parms);

        // Set the return type for the method. The return type is
        // specified by the second type parameter, U.
        demoMethod.SetReturnType(typeParameters[1]);

        // Generate a code body for the method. The method doesn't
        // do anything except return null.
        //
        ILGenerator ilgen = demoMethod.GetILGenerator();
        ilgen.Emit(OpCodes.Ldnull);
        ilgen.Emit(OpCodes.Ret);

        // Complete the type.
        Type dt = demoType.CreateType();

        // To bind types to a dynamic generic method, you must
        // first call the GetMethod method on the completed type.
        // You can then define an array of types, and bind them
        // to the method.
        MethodInfo m = dt.GetMethod("DemoMethod");
        Type[] typeArgs = {typeof(string), typeof(DemoMethodBuilder)};
        MethodInfo bound = m.MakeGenericMethod(typeArgs);
        // Display a string representing the bound method.
        Console.WriteLine(bound);

        // Save the assembly, so it can be examined with Ildasm.exe.
        demoAssembly.Save(asmName.Name + ".dll");
    }
}

/* This code example produces the following output:
Is DemoMethod generic? True
Is DemoMethod a generic method definition? True
DemoMethodBuilder DemoMethod[String,DemoMethodBuilder](System.String)
*/

備註

DefineGenericParameters呼叫 方法會讓目前的方法泛型。 無法復原這項變更。 第二次呼叫此方法會導致 InvalidOperationException

稍後 GetGenericArguments 可以使用 方法擷取泛型方法的類型參數。

根據慣例,類型參數名稱是單一大寫字母。

如需詳細資訊,請參閱 MethodBase.IsGenericMethodMethodInfo.GetGenericMethodDefinition。 如需泛型型別的資訊,請參閱 Type.IsGenericType

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

另請參閱