DynamicMethod.GetILGenerator 方法

定義

傳回可以用來發出動態方法之主體的 MSIL 產生器。

多載

GetILGenerator(Int32)

針對方法,傳回具有指定 Microsoft Intermediate Language (MSIL) 資料流大小的 MSIL 產生器。

GetILGenerator()

針對方法,傳回具有預設 Microsoft Intermediate Language (MSIL) 資料流大小 64 位元組的 MSIL 產生器。

GetILGenerator(Int32)

來源:
DynamicMethod.cs
來源:
DynamicMethod.CoreCLR.cs
來源:
DynamicMethod.CoreCLR.cs

針對方法,傳回具有指定 Microsoft Intermediate Language (MSIL) 資料流大小的 MSIL 產生器。

public System.Reflection.Emit.ILGenerator GetILGenerator(int streamSize);

參數

streamSize
Int32

MSIL 資料流的大小,以位元組為單位。

傳回

方法的 ILGenerator 物件,具有指定的 MSIL 資料流大小。

範例

下列程式代碼範例示範這個方法多載。 此程式代碼範例是針對 類別提供的較大範例的 DynamicMethod 一部分。

// Create an array that specifies the parameter types of the
// overload of Console.WriteLine to be used in Hello.
Type[] writeStringArgs = {typeof(string)};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo writeString = typeof(Console).GetMethod("WriteLine",
    writeStringArgs);

// Get an ILGenerator and emit a body for the dynamic method,
// using a stream size larger than the IL that will be
// emitted.
ILGenerator il = hello.GetILGenerator(256);
// Load the first argument, which is a string, onto the stack.
il.Emit(OpCodes.Ldarg_0);
// Call the overload of Console.WriteLine that prints a string.
il.EmitCall(OpCodes.Call, writeString, null);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret);

備註

在動態方法完成之後,藉由呼叫 CreateDelegateInvoke 方法,會忽略任何進一步嘗試新增 MSIL。 不會擲回任何例外狀況。

注意

動態方法中無法驗證的程式代碼有一些限制,即使在某些完全信任的案例中也是如此。 如需了解 DynamicMethod,請參閱<備註>中的<驗證>一節。

另請參閱

適用於

GetILGenerator()

來源:
DynamicMethod.cs
來源:
DynamicMethod.cs
來源:
DynamicMethod.cs

針對方法,傳回具有預設 Microsoft Intermediate Language (MSIL) 資料流大小 64 位元組的 MSIL 產生器。

public System.Reflection.Emit.ILGenerator GetILGenerator();

傳回

方法的 ILGenerator 物件。

範例

下列程式代碼範例會建立採用兩個參數的動態方法。 此範例會發出簡單的函式主體,將第一個參數列印至主控台,而範例會使用第二個參數做為 方法的傳回值。 此範例會建立委派、使用不同的參數叫用委派,最後使用 方法來叫用 Invoke 動態方法。

using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

public class Test
{
    // Declare a delegate that will be used to execute the completed
    // dynamic method.
    private delegate int HelloInvoker(string msg, int ret);

    public static void Main()
    {
        // Create an array that specifies the types of the parameters
        // of the dynamic method. This method has a string parameter
        // and an int parameter.
        Type[] helloArgs = {typeof(string), typeof(int)};

        // Create a dynamic method with the name "Hello", a return type
        // of int, and two parameters whose types are specified by the
        // array helloArgs. Create the method in the module that
        // defines the Test class.
        DynamicMethod hello = new DynamicMethod("Hello",
            typeof(int),
            helloArgs,
            typeof(Test).Module);

        // Create an array that specifies the parameter types of the
        // overload of Console.WriteLine to be used in Hello.
        Type[] writeStringArgs = {typeof(string)};
        // Get the overload of Console.WriteLine that has one
        // String parameter.
        MethodInfo writeString =
            typeof(Console).GetMethod("WriteLine", writeStringArgs);

        // Get an ILGenerator and emit a body for the dynamic method.
        ILGenerator il = hello.GetILGenerator();
        // Load the first argument, which is a string, onto the stack.
        il.Emit(OpCodes.Ldarg_0);
        // Call the overload of Console.WriteLine that prints a string.
        il.EmitCall(OpCodes.Call, writeString, null);
        // The Hello method returns the value of the second argument;
        // to do this, load the onto the stack and return.
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Ret);

        // Create a delegate that represents the dynamic method. This
        // action completes the method, and any further attempts to
        // change the method will cause an exception.
        HelloInvoker hi =
            (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));

        // Use the delegate to execute the dynamic method. Save and
        // print the return value.
        int retval = hi("\r\nHello, World!", 42);
        Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",
            retval);

        // Do it again, with different arguments.
        retval = hi("\r\nHi, Mom!", 5280);
        Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",
            retval);

        // Create an array of arguments to use with the Invoke method.
        object[] invokeArgs = {"\r\nHello, World!", 42};
        // Invoke the dynamic method using the arguments. This is much
        // slower than using the delegate, because you must create an
        // array to contain the arguments, and ValueType arguments
        // must be boxed.
        object objRet = hello.Invoke(null, invokeArgs);
        Console.WriteLine("hello.Invoke returned {0}", objRet);
    }
}

備註

在動態方法完成之後,藉由呼叫 CreateDelegateInvoke 方法,會忽略任何進一步嘗試新增 MSIL。 不會擲回任何例外狀況。

注意

動態方法中無法驗證的程式代碼有一些限制,即使在某些完全信任的案例中也是如此。 如需了解 DynamicMethod,請參閱<備註>中的<驗證>一節。

另請參閱

適用於