IL生成二维数组 [英] Generating IL for 2D Arrays
问题描述
我要生成IL的二维数组结构,使用 System.Reflection.Emit
命名空间。
I want to generate IL for the 2D array construction, using System.Reflection.Emit
namespace.
我的C#代码是
Array 2dArr = Array.CreateInstance(typeof(int),100,100);
使用反汇编
,我意识到,继IL对于上述
C#代码生成代码
Using ildasm
, I realized that following IL Code is generated for the above
C# code.
IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_000b: ldc.i4.s 100
IL_000d: ldc.i4.s 100
IL_000f: call class [mscorlib]System.Array [mscorlib]System.Array::CreateInstance(class [mscorlib]System.Type,
int32,
int32)
我是能够产生最后三个IL陈述如下。
I was able to generate last three IL statements as given below.
MethodInfo createArray = typeof(Array).GetMethod("CreateInstance",
new Type[] { typeof(Type),typeof(int),typeof(int) });
gen.Emit(OpCodes.Ldc_I4_1);
gen.Emit(OpCodes.Ldc_I4_1);
gen.Emit(OpCodes.Call, createArray);
但我没有对如何产生的拳头IL语句(即<$ C $清晰的思路C> IL_0006:调用类[mscorlib程序]的System.Type [mscorlib程序]的System.Type :: GetTypeFromHandle(值类型[mscorlib程序] System.RuntimeTypeHandle )
)
你有什么想法?
此外,有人点可以把一些好的教程/文件如何
使用的System.Reflection为了产生IL代码.Emit命名空间?
Furthermore, Can somebody point put some good tutorials/documents about how to Use System.Reflection.Emit namespace in order to generate IL codes?
推荐答案
嗯,好醇'的typeof
;是的,那就是:
Ah, good ol' typeof
; yes, that is:
il.Emit(OpCodes.Ldtoken, typeof(int));
il.EmitCall(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"), null);
重新引导......我的绝招,如果我遇到问题总是编译类似的东西,看看它反射。
Re guidance... my trick if I get stuck is always "compile something similar, and look at it in reflector".
如果你想要一些的例子的,的短小精悍点网和的 protobuf网都做IL,像样的数目 - 第一个是包含的多,有限的和可以理解的; 。二是全力以赴,无百无禁忌疯狂IL
If you wanted some examples, dapper-dot-net and protobuf-net both do a decent amount of IL - the first is more contained, limited and understandable; the second is all-out, no-holds-barred crazy IL.
有关IL提示:
- 跟踪注释堆在屏幕的右侧
- 使用树枝等短期形式的每一个步骤,但只有当你知道你使用它们有一个非常本地分支
- 写自己一点一套实用方法,即使对于像装载一个整数简单的事情(这实际上是相当复杂的,因为有12个不同的加载INT-32方法,根据值)
这篇关于IL生成二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!