英語で読む

次の方法で共有


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を含む動的型 を作成します。 このメソッドには 2 つのジェネリック型パラメーターがあり、そのうちの 1 つはパラメーターとして使用され、もう 1 つは戻り値の型として使用されます。

コードを実行すると、動的アセンブリが DemoGenericMethod1.dll という名前で保存され、Ildasm.exe (IL Disassembler) を使用してチェックできます。

注意

このコード例では、単に 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 現在のメソッドがジェネリックになります。 この変更を元に戻す方法はありません。 このメソッドを 2 回目に呼び出すと、 が発生します InvalidOperationException

ジェネリック メソッドの型パラメーターは、 メソッドを使用 GetGenericArguments して後で取得できます。

慣例により、型パラメーター名は 1 つの大文字です。

詳細については、次のトピックを参照してください。 MethodBase.IsGenericMethod および MethodInfo.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

こちらもご覧ください