4
我想使用System.Reflection.Emit
命名空间为2D阵列构造生成IL。为2D阵列生成IL
我的C#代码是
Array 2dArr = Array.CreateInstance(typeof(int),100,100);
使用ildasm
,我意识到,以下IL代码是上述 C#代码生成的。
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语句,如下所示。
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语句清晰的概念(即IL_0006: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle
) )
你有什么想法?
此外,有人可以指出一些关于如何使用System.Reflection.Emit命名空间以生成IL代码的好教程/文档?
非常感谢,这真的帮助我 – 2011-06-07 18:12:05