我正在尝试使用反射发射(IL 代码)制作自定义代理。一切正常,但是当我调用第二种方法时,我总是没有得到任何结果,并且我得到一个试图取消装箱空值(NullReferenceException)的执行。
这是使用 Emit 动态生成的 IL 代码。(我已经评论了重要的部分)
.method public hidebysig newslot virtual final
instance int32 TestCall(int32 A_1,
int32 A_2) cil managed
{
// Code size 155 (0x9b)
.maxstack 5
.locals init (class [mscorlib]System.Reflection.MethodInfo V_0,
class [BaseProxy.Console]BaseProxy.Console.TestClass V_1,
object[] V_2,
bool V_3,
object V_4)
IL_0000: ldc.i4 0x2
IL_0005: newarr [mscorlib]System.Object
IL_000a: stloc.2
IL_000b: ldloc.2
IL_000c: ldc.i4 0x0
IL_0011: ldarg A_1
IL_0015: nop
IL_0016: nop
IL_0017: box [mscorlib]System.Int32
IL_001c: stelem.ref
IL_001d: ldloc.2
IL_001e: ldc.i4 0x1
IL_0023: ldarg A_2
IL_0027: nop
IL_0028: nop
IL_0029: box [mscorlib]System.Int32
IL_002e: stelem.ref
IL_002f: ldtoken [BaseProxy.Console]BaseProxy.Console.TestClass
IL_0034: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0039: ldstr "TestCall"
IL_003e: call instance class [mscorlib]System.Reflection.MethodInfo [mscorlib]System.Type::GetMethod(string)
IL_0043: stloc.0
IL_0044: ldarg.0
IL_0045: ldfld class [BaseProxy.Console]BaseProxy.Console.TestClass TestClassProxy::wrappedObject
IL_004a: stloc.1
IL_004b: ldc.i4.1
IL_004c: stloc.3
IL_004d: ldarg.0
IL_004e: ldfld class [BaseProxy.Console]BaseProxy.Console.BasicInterceptor TestClassProxy::interception
IL_0053: ldloc.0
IL_0054: ldloc.2
IL_0055: ldloc.1
IL_0056: ldloca.s V_3
IL_0058: callvirt instance object [BaseProxy.Console]BaseProxy.Console.BasicInterceptor::Before(class [mscorlib]System.Reflection.MethodInfo,
object[],
object,
bool&)
**//Here everything is ok, i can get the result.**
IL_005d: box [mscorlib]System.Object
IL_0062: stloc.s V_4
IL_0064: nop
IL_0065: ldloc.3
IL_0066: brfalse.s IL_0093
IL_0068: ldarg.0
IL_0069: ldfld class [BaseProxy.Console]BaseProxy.Console.TestClass TestClassProxy::wrappedObject
IL_006e: ldloc.2
IL_006f: ldc.i4 0x0
IL_0074: ldelem.ref
IL_0075: unbox.any [mscorlib]System.Int32
IL_007a: ldloc.2
IL_007b: ldc.i4 0x1
IL_0080: ldelem.ref
IL_0081: unbox.any [mscorlib]System.Int32
IL_0086: callvirt instance int32 [BaseProxy.Console]BaseProxy.Console.TestClass::TestCall(int32,
int32)
**\\This Call Always return nothing (null)**
IL_008b: box [mscorlib]System.Object
IL_0090: stloc.s V_4
IL_0092: nop
IL_0093: ldloc.s V_4
**\\And i get a Exception here because loc 4 is null**
IL_0095: unbox.any [mscorlib]System.Int32
IL_009a: ret
} // end of method TestClassProxy::TestCall
TestCall 方法是一个非常简单的方法,它返回一个 int32 值。我在 TestCall 中放置了一个断点,并且它运行良好。
PEVerify 向我返回了以下错误:
[IL]: Error: [GeneratedProxyModule.dll : TestClassProxy::TestCall][offset 0x0000008B][found Int32][expected ref 'System.Object'] Unexpected type on the stack.
任何帮助都可以接受。提前致谢。