DynamicMethod.GetILGenerator 方法
定义
返回可用于发出动态方法的主体的 MSIL 生成器。Returns an MSIL generator that can be used to emit a body for the dynamic method.
重载
| GetILGenerator(Int32) |
为方法返回一个具有指定 MSIL 流大小的 Microsoft 中间语言 (MSIL) 生成器。Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size. |
| GetILGenerator() |
为具有默认 64 字节 Microsoft 中间语言 (MSIL) 流大小的方法返回 MSIL 生成器。Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream size of 64 bytes. |
GetILGenerator(Int32)
为方法返回一个具有指定 MSIL 流大小的 Microsoft 中间语言 (MSIL) 生成器。Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size.
public:
System::Reflection::Emit::ILGenerator ^ GetILGenerator(int streamSize);
public System.Reflection.Emit.ILGenerator GetILGenerator (int streamSize);
member this.GetILGenerator : int -> System.Reflection.Emit.ILGenerator
Public Function GetILGenerator (streamSize As Integer) As ILGenerator
参数
- streamSize
- Int32
MSIL 流的大小(以字节为单位)。The size of the MSIL stream, in bytes.
返回
方法的 ILGenerator 对象,具有指定的 MSIL 流大小。An ILGenerator object for the method, with the specified MSIL stream size.
示例
下面的代码示例演示了此方法重载。The following code example demonstrates this method overload. 此代码示例是为类提供的更大示例的一部分 DynamicMethod 。This code example is part of a larger example provided for the DynamicMethod class.
// Create an array that specifies the parameter types of the
// overload of Console::WriteLine to be used in Hello.
array<Type^>^ writeStringArgs = { String::typeid };
// Get the overload of Console::WriteLine that has one
// String parameter.
MethodInfo^ writeString = Console::typeid->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, nullptr);
// 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 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);
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(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.
Dim il As ILGenerator = 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, Nothing)
' 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)
注解
在通过调用或方法完成动态方法后, CreateDelegate Invoke 将忽略添加 MSIL 的任何进一步尝试。After a dynamic method has been completed, by calling the CreateDelegate or Invoke method, any further attempt to add MSIL is ignored. 不会引发异常。No exception is thrown.
备注
对于动态方法中的不可验证代码,即使在某些完全信任的情况下也存在限制。There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. 请参阅“验证”部分中的 DynamicMethod 注释。See the "Verification" section in Remarks for DynamicMethod.
适用于
GetILGenerator()
为具有默认 64 字节 Microsoft 中间语言 (MSIL) 流大小的方法返回 MSIL 生成器。Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream size of 64 bytes.
public:
System::Reflection::Emit::ILGenerator ^ GetILGenerator();
public System.Reflection.Emit.ILGenerator GetILGenerator ();
member this.GetILGenerator : unit -> System.Reflection.Emit.ILGenerator
Public Function GetILGenerator () As ILGenerator
返回
该方法的 ILGenerator 对象。An ILGenerator object for the method.
示例
下面的代码示例创建一个带有两个参数的动态方法。The following code example creates a dynamic method that takes two parameters. 该示例发出一个简单的函数体,将第一个参数输出到控制台,该示例使用第二个参数作为方法的返回值。The example emits a simple function body that prints the first parameter to the console, and the example uses the second parameter as the return value of the method. 该示例通过创建委托来完成方法,使用不同的参数调用委托,最后使用方法调用动态方法 Invoke 。The example completes the method by creating a delegate, invokes the delegate with different parameters, and finally invokes the dynamic method using the Invoke method.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public ref class Test
{
};
// Declare a delegate that will be used to execute the completed
// dynamic method.
delegate int HelloInvoker(String^ msg, int ret);
int 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.
array<Type^>^ helloArgs = {String::typeid, int::typeid};
// 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 = gcnew DynamicMethod("Hello",
int::typeid,
helloArgs,
Test::typeid->Module);
// Create an array that specifies the parameter types of the
// overload of Console.WriteLine to be used in Hello.
array<Type^>^ writeStringArgs = {String::typeid};
// Get the overload of Console.WriteLine that has one
// String parameter.
MethodInfo^ writeString =
Console::typeid->GetMethod("WriteLine", writeStringArgs);
// Get an ILGenerator and emit a body for the dynamic method.
ILGenerator^ ilgen = hello->GetILGenerator();
// Load the first argument, which is a string, onto the stack.
ilgen->Emit(OpCodes::Ldarg_0);
// Call the overload of Console.WriteLine that prints a string.
ilgen->EmitCall(OpCodes::Call, writeString, nullptr);
// The Hello method returns the value of the second argument;
// to do this, load the onto the stack and return.
ilgen->Emit(OpCodes::Ldarg_1);
ilgen->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^ helloDelegate =
(HelloInvoker^) hello->CreateDelegate(HelloInvoker::typeid);
// Use the delegate to execute the dynamic method. Save and
// print the return value.
int returnValue = helloDelegate("\r\nHello, World!", 42);
Console::WriteLine("helloDelegate(\"Hello, World!\", 42) returned {0}",
returnValue);
// Do it again, with different arguments.
returnValue = helloDelegate("\r\nHi, Mom!", 5280);
Console::WriteLine("helloDelegate(\"Hi, Mom!\", 5280) returned {0}",
returnValue);
// Create an array of arguments to use with the Invoke method.
array<Object^>^ delegateArgs = {"\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^ returnValueObject = hello->Invoke(nullptr, delegateArgs);
Console::WriteLine("hello.Invoke returned {0}", returnValueObject);
}
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);
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Public Class Test
' Declare a delegate that will be used to execute the completed
' dynamic method.
Private Delegate Function HelloInvoker(ByVal msg As String, _
ByVal ret As Integer) As Integer
Public Shared Sub Main()
' Create an array that specifies the types of the parameters
' of the dynamic method. This method has a String parameter
' and an Integer parameter.
Dim helloArgs() As Type = {GetType(String), GetType(Integer)}
' Create a dynamic method with the name "Hello", a return type
' of Integer, and two parameters whose types are specified by
' the array helloArgs. Create the method in the module that
' defines the Test class.
Dim hello As New DynamicMethod("Hello", _
GetType(Integer), _
helloArgs, _
GetType(Test).Module)
' Create an array that specifies the parameter types of the
' overload of Console.WriteLine to be used in Hello.
Dim writeStringArgs() As Type = {GetType(String)}
' Get the overload of Console.WriteLine that has one
' String parameter.
Dim writeString As MethodInfo = GetType(Console). _
GetMethod("WriteLine", writeStringArgs)
' Get an ILGenerator and emit a body for the dynamic method.
Dim il As ILGenerator = 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, Nothing)
' 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.
Dim hi As HelloInvoker = _
hello.CreateDelegate(GetType(HelloInvoker))
' Use the delegate to execute the dynamic method. Save and
' print the return value.
Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _
& retval)
' Do it again, with different arguments.
retval = hi(vbCrLf & "Hi, Mom!", 5280)
Console.WriteLine("Executing delegate hi(""Hi, Mom!"", 5280) returned " _
& retval)
' Create an array of arguments to use with the Invoke method.
Dim invokeArgs() As Object = {vbCrLf & "Hello, 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. Note that this overload of Invoke is
' inherited from MethodBase, and simply calls the more
' complete overload of Invoke.
Dim objRet As Object = hello.Invoke(Nothing, invokeArgs)
Console.WriteLine("hello.Invoke returned " & objRet)
End Sub
End Class
' This code example produces the following output:
'
'Hello, World!
'Executing delegate hi("Hello, World!", 42) returned 42
'
'Hi, Mom!
'Executing delegate hi("Hi, Mom!", 5280) returned 5280
'
'Hello, World!
'hello.Invoke returned 42
'
注解
在通过调用或方法完成动态方法后, CreateDelegate Invoke 将忽略添加 MSIL 的任何进一步尝试。After a dynamic method has been completed, by calling the CreateDelegate or Invoke method, any further attempt to add MSIL is ignored. 不会引发异常。No exception is thrown.
备注
对于动态方法中的不可验证代码,即使在某些完全信任的情况下也存在限制。There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. 请参阅“验证”部分中的 DynamicMethod 注释。See the "Verification" section in Remarks for DynamicMethod.