gpt4 book ai didi

c# - ILGenerator 捕获异常不起作用

转载 作者:可可西里 更新时间:2023-11-01 08:27:38 26 4
gpt4 key购买 nike

我正在使用 System.Reflection.Emit 为类型生成包装器。在某一时刻,原始对象可能会在访问时抛出错误 (FaultException),并且该错误应该被我的 try { } catch (Exception e) { } 捕获我已经实现了,但它没有。

代码由 ILSpy 正确显示.

try
{
if (original.Station != null)
{
if (objectDictionary.ContainsKey(original.Station))
{
this.Station = (objectDictionary[original.Station] as StationWrapper);
}
else
{
this.Station = new StationWrapper(original.Station, objectDictionary);
}
}
}
catch (Exception arg_6D_0)
{
ReportManager.Log(arg_6D_0);
}

代码生成

这是程序集生成的代码。

Label ex = il.BeginExceptionBlock();
....
// Exception block end
il.Emit(OpCodes.Leave, ex);
il.BeginCatchBlock(typeof(Exception));
il.Emit(OpCodes.Call, ReportManager_Log);
il.EndExceptionBlock();

编辑

异常被用户代码捕获,而不是被 IL 代码捕获。

反汇编

在这里删除了客户的一些命名空间。写入行是在最后几分钟添加的。

.try
{
IL_0019: ldarg.1
IL_001a: call instance class [...]...Station [...]...StationBase::get_Station()
IL_001f: brfalse IL_0063

IL_0024: ldarg.2
IL_0025: ldarg.1
IL_0026: call instance class [...]...Station [...]...StationBase::get_Station()
IL_002b: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2<object, object>::ContainsKey(!0)
IL_0030: brfalse IL_0051

IL_0035: ldarg.0
IL_0036: ldarg.2
IL_0037: ldarg.1
IL_0038: call instance class [...]...Station [...]...StationBase::get_Station()
IL_003d: call instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<object, object>::get_Item(!0)
IL_0042: isinst ...StationWrapper
IL_0047: call instance void ...StationBaseWrapper::set_Station(class ...StationWrapper)
IL_004c: br IL_0063

IL_0051: ldarg.0
IL_0052: ldarg.1
IL_0053: call instance class [...]...Station [...]...StationBase::get_Station()
IL_0058: ldarg.2
IL_0059: newobj instance void ....StationWrapper::.ctor(class [...]...Station, class [mscorlib]System.Collections.Generic.Dictionary`2<object, object>)
IL_005e: call instance void ...StationBaseWrapper::set_Station(class ...StationWrapper)

IL_0063: leave IL_007c
} // end .try
catch [mscorlib]System.Exception
{
IL_0068: ldstr "Its comming home"
IL_006d: call void [mscorlib]System.Console::WriteLine(string)
IL_0072: call void [...Report]...ReportManager::Log(class [mscorlib]System.Exception)
IL_0077: leave IL_007c
} // end handler

编辑2

当在 IL 代码中抛出 System.Exception 时,在 FaultException'1 发生之前,异常会得到处理。使用 ExceptionArgumentException 进行测试。

最佳答案

这里一切正常;您确定它不是嵌套的异常 block 吗?

例子:

using System;
using System.Reflection.Emit;

public class Test
{
static void Main()
{
var dm = new DynamicMethod("foo", null, new[] {typeof(bool)});
var il = dm.GetILGenerator();

Label ex = il.BeginExceptionBlock();
il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Call, typeof(Test).GetMethod("Throw"), null);
il.Emit(OpCodes.Leave, ex);

il.BeginCatchBlock(typeof(Exception));
il.EmitCall(OpCodes.Call, typeof(Test).GetMethod("Log"), null);
il.EndExceptionBlock();
il.Emit(OpCodes.Ldstr, "done");
il.EmitCall(OpCodes.Call, typeof(Console).GetMethod("WriteLine",
new[] {typeof(string)}), null);

il.Emit(OpCodes.Ret);
var act = (Action<bool>)dm.CreateDelegate(typeof (Action<bool>));
Console.WriteLine("Expect success:");
act(false);
Console.WriteLine("Expect fail:");
act(true);
Console.WriteLine("(all done)");
}
public static void Throw(bool fatal)
{
if(fatal) throw new InvalidOperationException("Boom!");
}
public static void Log(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}

关于c# - ILGenerator 捕获异常不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9375221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com