导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

DynamicMethod.GetILGenerator 方法 (Int32)

2013/12/13

为方法返回一个具有指定 MSIL 流大小的 Microsoft 中间语言 (MSIL) 生成器。

Namespace:  System.Reflection.Emit
程序集:  mscorlib(位于 mscorlib.dll 中)

public ILGenerator GetILGenerator(
	int streamSize
)

参数

streamSize
类型: System.Int32
MSIL 流的大小,以字节为单位。

返回值

类型: System.Reflection.Emit.ILGenerator
方法的 ILGenerator 对象,具有指定的 MSIL 流大小。

通过调用 CreateDelegateInvoke 方法完成动态方法之后,将忽略所有尝试添加 MSIL 的进一步尝试。不引发异常。

下面的代码示例演示此方法重载。此代码示例摘自一个为 DynamicMethod 类提供的更大示例。


// Create an array that specifies the parameter types of the
// overload of String.Format to be used in Hello.
Type[] formatStringArgs = { typeof(string), typeof(object), typeof(object), 
                           typeof(object) };
// Get the overload of String.Format that has one String 
// parameter for the format, and three object parameters.
MethodInfo formatString = typeof(String).GetMethod("Format",
    formatStringArgs);

// 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);
// Push the format string for String.Format on the stack.
il.Emit(OpCodes.Ldstr, "Hello, {0}, {1} squared is {2}!");
// Load the first argument, which is a string, onto the stack,
// as the second argument of String.Format.
il.Emit(OpCodes.Ldarg_0);
// Load the second argument, and box it. This is the third argument
// of String.Format, and because it is a value type it must be boxed
// in order to pass it as an object.
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Box, typeof(int));
// Load the second argument twice more, as the arguments of the 
// multiply operation that will square it. 
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_1);
// Multiply the last two arguments on the stack, leaving the 
// result on the stack. The result must be boxed, so it can be passed
// as the fourth argument of String.Format.
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Box, typeof(int));
// Call the overload of String.Format that prints a string, formatting
// it to include the next three objects on the stack.
il.Emit(OpCodes.Call, formatString);
// The Hello method returns the formatted string, which is already
// on the stack after the call to String.Format.
il.Emit(OpCodes.Ret);


Windows Phone OS

受以下版本支持: 8.0, 7.1

Windows Phone

显示:
© 2014 Microsoft