祝大家日安!
我使用Mono.Cecil复制了一个方法,并遇到了以下问题:
我的复制函数很简单(例如):
public static bool Ping()
{
MessageBox.Show("Ping");
try
{
MessageBox.Show("status");
}
catch (Exception exception)
{
MessageBox.Show("Exception ");
return false;
}
return true;
}这是工作而不是问题。但如果我这么做了:
MessageBox.Show("Exception " + exception.Message);我遇到了麻烦:当执行到达函数调用时,它会生成“未处理的异常”,clr就会结束工作。我甚至都看不到MessageBox.Show("Ping")!
我复制try/catch块,这样:
foreach (ExceptionHandler exh in SourceMethod.Body.ExceptionHandlers)
{
var ex = new ExceptionHandler(exh.HandlerType)
{
TryStart = exh.TryStart,
TryEnd = exh.TryEnd,
HandlerStart = exh.TryEnd,
HandlerEnd = exh.HandlerEnd,
CatchType = Assembly.MainModule.Import(typeof(Exception)),
HandlerType = exh.HandlerType,
FilterStart = exh.FilterStart
};
target.Body.ExceptionHandlers.Add(ex);
}我想我的问题在这里:
CatchType = Assembly.MainModule.Import(typeof(Exception)), 但我不知道该怎么做
我试着:
CatchType = exh.CatchType 但不幸的是。
如何解决这种情况?有什么想法吗?
发布于 2021-02-18 21:18:51
很可能您正在生成无效的IL,而运行时正在检测到这一点,甚至没有开始运行您的程序(此时您看不到ping)。
您可以通过运行以下命令来检查这一点(至少在Windows上):
peverify程序集路径
也就是说,您可以在这里看到一个完整的示例:https://cecilifier.me/?gistid=fd87051cdca0860916a171c419a30f80
https://stackoverflow.com/questions/51019113
复制相似问题