英語で読む

次の方法で共有


方法: リフレクション出力を使用してジェネリック メソッドを定義する

最初の手順では、2 つの型パラメーターを持つ単純なジェネリック メソッドを作成する方法と、クラスの制約、インターフェイスの制約、および特殊な制約を型パラメーターに適用する方法を示します。

2 番目の手順では、メソッド本体を出力する方法と、ジェネリック メソッドの型パラメーターを使用して、ジェネリック型のインスタンスを作成し、それらのメソッドを呼び出す方法を示します。

3 番目の手順では、ジェネリック メソッドを呼び出す方法を示します。

重要

メソッドはジェネリック型に属し、その型の型パラメーターを使用するだけであるため、ジェネリックではありません。 メソッドがジェネリックになるのは、そのメソッドが独自の型パラメーター リストを持つ場合だけです。 この例のように、ジェネリック メソッドは非ジェネリック型で表すことができます。 ジェネリック型の非ジェネリック メソッドの例については、「方法:リフレクション出力を使用してジェネリック型を定義する」をご覧ください。

ジェネリック メソッドを定義する

  1. 作業を始める前に、高水準言語を使用して記述したときに、ジェネリック メソッドがどのように表されるかを確認しておくと役立ちます。 次のコードは、ジェネリック メソッドを呼び出すコードと共に、この記事のプログラム例に含まれています。 このメソッドは、TInputTOutput の 2 つの型パラメーターを持ちます。2 つ目の型パラメーターは、参照型 (class) であることが必要です。さらに、パラメータなしのコンストラクター (new) を持ち、ICollection<TInput> を実装する必要があります。 このインターフェイスの制約によって、ICollection<T>.Add メソッドを使用して、メソッドによって作成される TOutput コレクションに要素を確実に追加できるようになります。 メソッドは、input の配列である仮パラメーター TInput を 1 つ持ちます。 メソッドは、TOutput 型のコレクションを作成し、input の要素をコレクションにコピーします。

    C#
    public static TOutput Factory<TInput, TOutput>(TInput[] tarray)
        where TOutput : class, ICollection<TInput>, new()
    {
        TOutput ret = new TOutput();
        ICollection<TInput> ic = ret;
    
        foreach (TInput t in tarray)
        {
            ic.Add(t);
        }
        return ret;
    }
    
  2. 動的アセンブリと動的モジュールを定義して、ジェネリック メソッドが属する型を格納します。 この場合、アセンブリには DemoMethodBuilder1 という名前のモジュールが 1 つだけ含まれます。モジュール名はアセンブリ名に拡張子が付いたものです。 この例では、アセンブリをディスクに保存したうえで実行するため、AssemblyBuilderAccess.RunAndSave を指定します。 Ildasm.exe (IL 逆アセンブラー) を使用すると、DemoMethodBuilder1.dll をチェックし、ステップ 1 に示すメソッドの共通中間言語 (CIL) と比較できます。

    C#
    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");
    
  3. ジェネリック メソッドが属する型を定義します。 型はジェネリックである必要はありません。 ジェネリック メソッドは、ジェネリック型と非ジェネリック型のどちらにでも属することができます。 この例では、型はクラスであり、ジェネリックではありません。型の名前は DemoType です。

    C#
    TypeBuilder demoType =
        demoModule.DefineType("DemoType", TypeAttributes.Public);
    
  4. ジェネリック メソッドを定義します。 ジェネリック メソッドの仮パラメーターの型が、ジェネリック メソッドのジェネリック型パラメーターで指定されている場合は、DefineMethod(String, MethodAttributes) メソッド オーバーロードを使用してメソッドを定義します。 メソッドのジェネリック型パラメーターはまだ定義されていないため、DefineMethod の呼び出しでメソッドの仮パラメーターの型を指定することはできません。 この例では、メソッドの名前は Factory です。 メソッドは、パブリックかつ static (Visual Basic では Shared) です。

    C#
    MethodBuilder factory =
        demoType.DefineMethod("Factory",
            MethodAttributes.Public | MethodAttributes.Static);
    
  5. パラメーターの名前を格納している文字列の配列を DemoMethod メソッドに渡して、MethodBuilder.DefineGenericParameters のジェネリック型パラメーターを定義します。 これにより、メソッドはジェネリック メソッドになります。 次のコードによって、Factory を型パラメーター TInputTOutput を持つジェネリック メソッドにします。 コードを読みやすくするために、これらの名前の変数を作成し、2 つの型パラメーターを表す GenericTypeParameterBuilder オブジェクトを保持します。

    C#
    string[] typeParameterNames = {"TInput", "TOutput"};
    GenericTypeParameterBuilder[] typeParameters =
        factory.DefineGenericParameters(typeParameterNames);
    
    GenericTypeParameterBuilder TInput = typeParameters[0];
    GenericTypeParameterBuilder TOutput = typeParameters[1];
    
  6. オプションで、型パラメーターに特殊な制約を追加します。 特殊な制約は、SetGenericParameterAttributes メソッドを使用して追加します。 この例では、TOutput は参照型であり、パラメーターなしのコンストラクターを持つように制約されます。

    C#
    TOutput.SetGenericParameterAttributes(
        GenericParameterAttributes.ReferenceTypeConstraint |
        GenericParameterAttributes.DefaultConstructorConstraint);
    
  7. オプションで、型パラメーターにクラスの制約とインターフェイスの制約を追加します。 この例では、型パラメーター TOutput は、ICollection(Of TInput) (C# では ICollection<TInput>) インターフェイスを実装する型に制約されます。 これにより、Add メソッドを使用して要素を確実に追加できます。

    C#
    Type icoll = typeof(ICollection<>);
    Type icollOfTInput = icoll.MakeGenericType(TInput);
    Type[] constraints = {icollOfTInput};
    TOutput.SetInterfaceConstraints(constraints);
    
  8. SetParameters メソッドを使用して、メソッドの仮パラメーターを定義します。 この例では、Factory メソッドは、TInput の配列であるパラメーターを 1 つ持ちます。 この型は、MakeArrayType を表す GenericTypeParameterBuilderTInput メソッドを呼び出すことによって作成されます。 SetParameters の引数は、Type オブジェクトの配列です。

    C#
    Type[] parms = {TInput.MakeArrayType()};
    factory.SetParameters(parms);
    
  9. SetReturnType メソッドを使用して、メソッドの戻り値の型を定義します。 この例では、TOutput のインスタンスが返されます。

    C#
    factory.SetReturnType(TOutput);
    
  10. ILGenerator を使用して、メソッド本体を出力します。 詳細については、メソッド本体を出力する際の手順を参照してください。

    重要

    ジェネリック型のメソッド呼び出しを出力し、それらの型の型引数がジェネリック メソッドの型パラメーターである場合、static クラスの GetConstructor(Type, ConstructorInfo)GetMethod(Type, MethodInfo)GetField(Type, FieldInfo)TypeBuilder の各メソッド オーバーロードを使用して、メソッドの構築された形式を取得します。 メソッド本体を出力する手順にこの例が示されています。

  11. メソッドを格納する型を完成させ、アセンブリを保存します。 ジェネリック メソッドを呼び出すための手順には、完成したメソッドを呼び出す 2 とおりの方法が示されています。

    C#
    // Complete the type.
    Type dt = demoType.CreateType();
    // Save the assembly, so it can be examined with Ildasm.exe.
    demoAssembly.Save(asmName.Name+".dll");
    

メソッド本体を出力する

  1. コード ジェネレーターを取得し、ローカル変数とラベルを宣言します。 ローカル変数を宣言するには、DeclareLocal メソッドを使用します。 Factory メソッドには、4 つのローカル変数があります。メソッドによって返される新しい TOutput を保持する retValICollection<TInput> にキャストされた TOutput を保持する icTInput オブジェクトの入力配列を保持する input、および配列を反復処理するための index です。 また、ラベルも 2 つあります。ループに入るためのラベル (enterLoop) およびループの先頭に使用するラベル (loopAgain) であり、DefineLabel メソッドを使用して定義されます。

    メソッドは、まず、Ldarg_0 オペコードを使用して引数を読み込み、input オペコードを使用してローカル変数 Stloc_S に格納します。

    C#
    ILGenerator ilgen = factory.GetILGenerator();
    
    LocalBuilder retVal = ilgen.DeclareLocal(TOutput);
    LocalBuilder ic = ilgen.DeclareLocal(icollOfTInput);
    LocalBuilder input = ilgen.DeclareLocal(TInput.MakeArrayType());
    LocalBuilder index = ilgen.DeclareLocal(typeof(int));
    
    Label enterLoop = ilgen.DefineLabel();
    Label loopAgain = ilgen.DefineLabel();
    
    ilgen.Emit(OpCodes.Ldarg_0);
    ilgen.Emit(OpCodes.Stloc_S, input);
    
  2. TOutput メソッドのジェネリック メソッド オーバーロードを使用して、Activator.CreateInstance のインスタンスを作成するコードを出力します。 このオーバーロードを使用する場合、指定した型はパラメーターなしのコンストラクターを持つ必要があります。TOutput にこの制約を追加したのはこのためです。 TOutputMakeGenericMethod に渡して、構築されたジェネリック メソッドを作成します。 メソッドを呼び出すコードを出力したら、retVal を使用してローカル変数 Stloc_S に格納するコードを出力します。

    C#
    MethodInfo createInst =
        typeof(Activator).GetMethod("CreateInstance", Type.EmptyTypes);
    MethodInfo createInstOfTOutput =
        createInst.MakeGenericMethod(TOutput);
    
    ilgen.Emit(OpCodes.Call, createInstOfTOutput);
    ilgen.Emit(OpCodes.Stloc_S, retVal);
    
  3. 新しい TOutput オブジェクトを ICollection(Of TInput) にキャストし、ローカル変数 ic に格納するコードを出力します。

    C#
    ilgen.Emit(OpCodes.Ldloc_S, retVal);
    ilgen.Emit(OpCodes.Box, TOutput);
    ilgen.Emit(OpCodes.Castclass, icollOfTInput);
    ilgen.Emit(OpCodes.Stloc_S, ic);
    
  4. MethodInfo メソッドを表す ICollection<T>.Add を取得します。 このメソッドは、ICollection<TInput> で動作するため、この構築された型に固有の Add メソッドを取得する必要があります。 GetMethod は、MethodInfo で構築された型ではサポートされていないため、icollOfTInput メソッドを使用して、GetMethod からこの GenericTypeParameterBuilder を直接取得することはできません。 代わりに、GetMethod ジェネリック インターフェイスのジェネリック型の定義を格納する icoll で、ICollection<T> を呼び出します。 次に、GetMethod(Type, MethodInfo)static メソッドを使用して、構築された型の MethodInfo を作成します。 次のコードでこれを示します。

    C#
    MethodInfo mAddPrep = icoll.GetMethod("Add");
    MethodInfo mAdd = TypeBuilder.GetMethod(icollOfTInput, mAddPrep);
    
  5. 32 ビット整数 0 を読み込み、変数に格納することにより、変数 index を初期化するコードを出力します。 enterLoop ラベルに分岐するコードを出力します。 このラベルはループ内部にあるため、まだマークされていません。 ループのコードは、次の手順で出力されます。

    C#
    // Initialize the count and enter the loop.
    ilgen.Emit(OpCodes.Ldc_I4_0);
    ilgen.Emit(OpCodes.Stloc_S, index);
    ilgen.Emit(OpCodes.Br_S, enterLoop);
    
  6. ループのコードを出力します。 まず、MarkLabel ラベルを使用して loopAgain を呼び出し、ループの先頭をマークします。 これで、このラベルを使用する分岐ステートメントは、コードのこのポイントに分岐するようになります。 次の手順では、TOutput にキャストされた ICollection(Of TInput) オブジェクトをスタックにプッシュします。 このオブジェクトはすぐに必要なわけではありませんが、Add メソッドを呼び出すための適切な位置にあることが必要です。 次に、入力配列がスタックにプッシュされ、現在のインデックスを格納する変数 index が配列にプッシュされます。 Ldelem オペコードによってインデックスと配列がスタックからポップされ、インデックスが付けられた配列要素がスタックにプッシュされます。 これで、スタックは ICollection<T>.Add メソッドの呼び出しに対応できるようになりました。このメソッドは、スタックからコレクションと新しい要素をポップし、その要素をコレクションに追加します。

    ループのコードの残りの部分では、インデックスをインクリメントし、ループが終了したかどうかを確認するテストを行います。インデックスと 32 ビット整数 1 がスタックにプッシュされて加算され、その合計がスタックに残されます。合計は index に格納されます。 MarkLabel を呼び出して、このポイントをループのエントリ ポイントとして設定します。 インデックスが再び読み込まれます。 入力配列がスタックにプッシュされ、その長さを取得するために Ldlen が出力されます。 現在、インデックスと長さはスタックにあります。Clt を出力してこれらを比較します。 インデックスが長さ未満の場合、Brtrue_S はループの先頭に分岐して戻ります。

    C#
    ilgen.MarkLabel(loopAgain);
    
    ilgen.Emit(OpCodes.Ldloc_S, ic);
    ilgen.Emit(OpCodes.Ldloc_S, input);
    ilgen.Emit(OpCodes.Ldloc_S, index);
    ilgen.Emit(OpCodes.Ldelem, TInput);
    ilgen.Emit(OpCodes.Callvirt, mAdd);
    
    ilgen.Emit(OpCodes.Ldloc_S, index);
    ilgen.Emit(OpCodes.Ldc_I4_1);
    ilgen.Emit(OpCodes.Add);
    ilgen.Emit(OpCodes.Stloc_S, index);
    
    ilgen.MarkLabel(enterLoop);
    ilgen.Emit(OpCodes.Ldloc_S, index);
    ilgen.Emit(OpCodes.Ldloc_S, input);
    ilgen.Emit(OpCodes.Ldlen);
    ilgen.Emit(OpCodes.Conv_I4);
    ilgen.Emit(OpCodes.Clt);
    ilgen.Emit(OpCodes.Brtrue_S, loopAgain);
    
  7. TOutput オブジェクトをスタックにプッシュし、メソッドから返すコードを出力します。 ローカル変数 retValic には、新しい TOutput への参照が格納されます。ic は、ICollection<T>.Add メソッドにアクセスする目的でのみ使用されます。

    C#
    ilgen.Emit(OpCodes.Ldloc_S, retVal);
    ilgen.Emit(OpCodes.Ret);
    

ジェネリック メソッドを呼び出す

  1. Factory は、ジェネリック メソッドの定義です。 これを呼び出すには、ジェネリック型パラメーターに型を割り当てる必要があります。 これを行うには、MakeGenericMethod メソッドを使用します。 次のコードでは、StringTInputList(Of String)List<string> (C# では TOutput) を指定して、構築されたジェネリック メソッドを作成し、メソッドの文字列形式を表示します。

    C#
    MethodInfo m = dt.GetMethod("Factory");
    MethodInfo bound =
        m.MakeGenericMethod(typeof(string), typeof(List<string>));
    
    // Display a string representing the bound method.
    Console.WriteLine(bound);
    
  2. 遅延バインディングによってメソッドを呼び出すには、Invoke メソッドを使用します。 次のコードでは、唯一の要素として文字列の配列を格納する Object の配列を作成し、ジェネリック メソッドの引数リストとして渡します。 このメソッドは Invoke であるため、static の 1 つ目のパラメーターは null 参照です。 戻り値は List(Of String) にキャストされ、最初の要素が表示されます。

    C#
    object o = bound.Invoke(null, new object[]{arr});
    List<string> list2 = (List<string>) o;
    
    Console.WriteLine("The first element is: {0}", list2[0]);
    
  3. デリゲートを使用してメソッドを呼び出すには、構築されたジェネリック メソッドのシグネチャと一致するデリゲートを用意する必要があります。 汎用デリゲートを作成すると、これを簡単に行うことができます。 次のコードでは、D メソッド オーバーロードを使用して、プログラム例で定義した Delegate.CreateDelegate(Type, MethodInfo) 汎用デリゲートのインスタンスを作成し、デリゲートを呼び出します。 デリゲートは、遅延バインディングによる呼び出しよりもパフォーマンスが優れています。

    C#
    Type dType = typeof(D<string, List <string>>);
    D<string, List <string>> test;
    test = (D<string, List <string>>)
        Delegate.CreateDelegate(dType, bound);
    
    List<string> list3 = test(arr);
    Console.WriteLine("The first element is: {0}", list3[0]);
    
  4. 出力されたメソッドは、保存されたアセンブリを参照するプログラムから呼び出すこともできます。

ジェネリック メソッド DemoType と共に、非ジェネリック型 Factory を作成するコード例を次に示します。 このメソッドは、入力の種類を指定する TInput と、出力の種類を指定する TOutput の 2 つのジェネリック型パラメーターを持ちます。 型パラメーター TOutput は、ICollection<TInput> (Visual Basic では ICollection(Of TInput)) を実装し、参照型であり、パラメーターなしのコンストラクターを持つように制約されています。

メソッドは、TInput の配列である仮パラメーターを 1 つ持ちます。 メソッドは、入力配列のすべての要素を格納する TOutput のインスタンスを返します。 TOutputICollection<T> ジェネリック インターフェイスを実装するジェネリック コレクション型に指定できます。

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

注意

コードの出力方法を習得するためのよい方法は、出力しようとしているタスクを実行するプログラムを記述し、逆アセンブラーを使用して、コンパイラによって生成された CIL をチェックすることです。

このコード例には、出力されたメソッドに相当するソース コードが含まれています。 出力されたメソッドは、遅延バインディングによって呼び出されます。また、このコード例で宣言された汎用デリゲートを使用することによっても呼び出されます。

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

// Declare a generic delegate that can be used to execute the
// finished method.
//
public delegate TOut D<TIn, TOut>(TIn[] input);

class GenericMethodBuilder
{
    // This method shows how to declare, in Visual Basic, the generic
    // method this program emits. The method has two type parameters,
    // TInput and TOutput, the second of which must be a reference type
    // (class), must have a parameterless constructor (new()), and must
    // implement ICollection<TInput>. This interface constraint
    // ensures that ICollection<TInput>.Add can be used to add
    // elements to the TOutput object the method creates. The method
    // has one formal parameter, input, which is an array of TInput.
    // The elements of this array are copied to the new TOutput.
    //
    public static TOutput Factory<TInput, TOutput>(TInput[] tarray)
        where TOutput : class, ICollection<TInput>, new()
    {
        TOutput ret = new TOutput();
        ICollection<TInput> ic = ret;

        foreach (TInput t in tarray)
        {
            ic.Add(t);
        }
        return ret;
    }

    public static void Main()
    {
        // The following shows the usage syntax of the C#
        // version of the generic method emitted by this program.
        // Note that the generic parameters must be specified
        // explicitly, because the compiler does not have enough
        // context to infer the type of TOutput. In this case, TOutput
        // is a generic List containing strings.
        //
        string[] arr = {"a", "b", "c", "d", "e"};
        List<string> list1 =
            GenericMethodBuilder.Factory<string, List <string>>(arr);
        Console.WriteLine("The first element is: {0}", list1[0]);

        // 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");

        // Define a type to contain the method.
        TypeBuilder demoType =
            demoModule.DefineType("DemoType", TypeAttributes.Public);

        // Define a public static 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 factory =
            demoType.DefineMethod("Factory",
                MethodAttributes.Public | MethodAttributes.Static);

        // Defining generic type parameters for the method makes it a
        // generic method. To make the code easier to read, each
        // type parameter is copied to a variable of the same name.
        //
        string[] typeParameterNames = {"TInput", "TOutput"};
        GenericTypeParameterBuilder[] typeParameters =
            factory.DefineGenericParameters(typeParameterNames);

        GenericTypeParameterBuilder TInput = typeParameters[0];
        GenericTypeParameterBuilder TOutput = typeParameters[1];

        // Add special constraints.
        // The type parameter TOutput is constrained to be a reference
        // type, and to have a parameterless constructor. This ensures
        // that the Factory method can create the collection type.
        //
        TOutput.SetGenericParameterAttributes(
            GenericParameterAttributes.ReferenceTypeConstraint |
            GenericParameterAttributes.DefaultConstructorConstraint);

        // Add interface and base type constraints.
        // The type parameter TOutput is constrained to types that
        // implement the ICollection<T> interface, to ensure that
        // they have an Add method that can be used to add elements.
        //
        // To create the constraint, first use MakeGenericType to bind
        // the type parameter TInput to the ICollection<T> interface,
        // returning the type ICollection<TInput>, then pass
        // the newly created type to the SetInterfaceConstraints
        // method. The constraints must be passed as an array, even if
        // there is only one interface.
        //
        Type icoll = typeof(ICollection<>);
        Type icollOfTInput = icoll.MakeGenericType(TInput);
        Type[] constraints = {icollOfTInput};
        TOutput.SetInterfaceConstraints(constraints);

        // Set parameter types for the method. The method takes
        // one parameter, an array of type TInput.
        Type[] parms = {TInput.MakeArrayType()};
        factory.SetParameters(parms);

        // Set the return type for the method. The return type is
        // the generic type parameter TOutput.
        factory.SetReturnType(TOutput);

        // Generate a code body for the method.
        // -----------------------------------
        // Get a code generator and declare local variables and
        // labels. Save the input array to a local variable.
        //
        ILGenerator ilgen = factory.GetILGenerator();

        LocalBuilder retVal = ilgen.DeclareLocal(TOutput);
        LocalBuilder ic = ilgen.DeclareLocal(icollOfTInput);
        LocalBuilder input = ilgen.DeclareLocal(TInput.MakeArrayType());
        LocalBuilder index = ilgen.DeclareLocal(typeof(int));

        Label enterLoop = ilgen.DefineLabel();
        Label loopAgain = ilgen.DefineLabel();

        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Stloc_S, input);

        // Create an instance of TOutput, using the generic method
        // overload of the Activator.CreateInstance method.
        // Using this overload requires the specified type to have
        // a parameterless constructor, which is the reason for adding
        // that constraint to TOutput. Create the constructed generic
        // method by passing TOutput to MakeGenericMethod. After
        // emitting code to call the method, emit code to store the
        // new TOutput in a local variable.
        //
        MethodInfo createInst =
            typeof(Activator).GetMethod("CreateInstance", Type.EmptyTypes);
        MethodInfo createInstOfTOutput =
            createInst.MakeGenericMethod(TOutput);

        ilgen.Emit(OpCodes.Call, createInstOfTOutput);
        ilgen.Emit(OpCodes.Stloc_S, retVal);

        // Load the reference to the TOutput object, cast it to
        // ICollection<TInput>, and save it.
        //
        ilgen.Emit(OpCodes.Ldloc_S, retVal);
        ilgen.Emit(OpCodes.Box, TOutput);
        ilgen.Emit(OpCodes.Castclass, icollOfTInput);
        ilgen.Emit(OpCodes.Stloc_S, ic);

        // Loop through the array, adding each element to the new
        // instance of TOutput. Note that in order to get a MethodInfo
        // for ICollection<TInput>.Add, it is necessary to first
        // get the Add method for the generic type defintion,
        // ICollection<T>.Add. This is because it is not possible
        // to call GetMethod on icollOfTInput. The static overload of
        // TypeBuilder.GetMethod produces the correct MethodInfo for
        // the constructed type.
        //
        MethodInfo mAddPrep = icoll.GetMethod("Add");
        MethodInfo mAdd = TypeBuilder.GetMethod(icollOfTInput, mAddPrep);

        // Initialize the count and enter the loop.
        ilgen.Emit(OpCodes.Ldc_I4_0);
        ilgen.Emit(OpCodes.Stloc_S, index);
        ilgen.Emit(OpCodes.Br_S, enterLoop);

        // Mark the beginning of the loop. Push the ICollection
        // reference on the stack, so it will be in position for the
        // call to Add. Then push the array and the index on the
        // stack, get the array element, and call Add (represented
        // by the MethodInfo mAdd) to add it to the collection.
        //
        // The other ten instructions just increment the index
        // and test for the end of the loop. Note the MarkLabel
        // method, which sets the point in the code where the
        // loop is entered. (See the earlier Br_S to enterLoop.)
        //
        ilgen.MarkLabel(loopAgain);

        ilgen.Emit(OpCodes.Ldloc_S, ic);
        ilgen.Emit(OpCodes.Ldloc_S, input);
        ilgen.Emit(OpCodes.Ldloc_S, index);
        ilgen.Emit(OpCodes.Ldelem, TInput);
        ilgen.Emit(OpCodes.Callvirt, mAdd);

        ilgen.Emit(OpCodes.Ldloc_S, index);
        ilgen.Emit(OpCodes.Ldc_I4_1);
        ilgen.Emit(OpCodes.Add);
        ilgen.Emit(OpCodes.Stloc_S, index);

        ilgen.MarkLabel(enterLoop);
        ilgen.Emit(OpCodes.Ldloc_S, index);
        ilgen.Emit(OpCodes.Ldloc_S, input);
        ilgen.Emit(OpCodes.Ldlen);
        ilgen.Emit(OpCodes.Conv_I4);
        ilgen.Emit(OpCodes.Clt);
        ilgen.Emit(OpCodes.Brtrue_S, loopAgain);

        ilgen.Emit(OpCodes.Ldloc_S, retVal);
        ilgen.Emit(OpCodes.Ret);

        // Complete the type.
        Type dt = demoType.CreateType();
        // Save the assembly, so it can be examined with Ildasm.exe.
        demoAssembly.Save(asmName.Name+".dll");

        // To create a constructed generic method that can be
        // executed, first call the GetMethod method on the completed
        // type to get the generic method definition. Call MakeGenericType
        // on the generic method definition to obtain the constructed
        // method, passing in the type arguments. In this case, the
        // constructed method has string for TInput and List<string>
        // for TOutput.
        //
        MethodInfo m = dt.GetMethod("Factory");
        MethodInfo bound =
            m.MakeGenericMethod(typeof(string), typeof(List<string>));

        // Display a string representing the bound method.
        Console.WriteLine(bound);

        // Once the generic method is constructed,
        // you can invoke it and pass in an array of objects
        // representing the arguments. In this case, there is only
        // one element in that array, the argument 'arr'.
        //
        object o = bound.Invoke(null, new object[]{arr});
        List<string> list2 = (List<string>) o;

        Console.WriteLine("The first element is: {0}", list2[0]);

        // You can get better performance from multiple calls if
        // you bind the constructed method to a delegate. The
        // following code uses the generic delegate D defined
        // earlier.
        //
        Type dType = typeof(D<string, List <string>>);
        D<string, List <string>> test;
        test = (D<string, List <string>>)
            Delegate.CreateDelegate(dType, bound);

        List<string> list3 = test(arr);
        Console.WriteLine("The first element is: {0}", list3[0]);
    }
}

/* This code example produces the following output:

The first element is: a
System.Collections.Generic.List`1[System.String] Factory[String,List`1](System.String[])
The first element is: a
The first element is: a
 */

関連項目