单步进入通过反射调用的dll的源代码

发布于 2024-10-25 23:21:52 字数 703 浏览 5 评论 0原文

我试图通过反射调用 .dll 的方法,但收到 TargetInvocableException。该场景的工作原理如下:我有一个名为 Labor.dll 的 dll。它包含多个文件,其中包括 Demand.csRole.cs 。我可以成功单步执行 Demand.cs 中定义的函数,但每当我尝试单步执行 Role.cs 中定义的函数时,我都会收到 TargetInvocationException。但是,我不应该认为此错误发生在 Role.cs 方法内部,因此 .Net 可以找到该方法并从中执行,它只是不会告诉我它在做什么。

如何单步执行 Role.cs 中定义的方法,或者什么会阻止我查看该代码?

额外信息:

  • Labor.dll 正在通过反射加载和调用。
  • 当我在 Labor.dll 上使用 Reflector 时,我可以查看 Role.cs 中定义的方法
  • Role.cs 中的方法在以下情况下抛出错误它执行我认为不应该执行的代码,这就是为什么我更专注于单步执行代码而不是防止 TargetInitationException

I am trying to call a .dll's method via reflection, but am getting a TargetInvocationException. The scenario works like this: I have a dll called Labor.dll. It holds several files, among them Demand.cs and Role.cs. I can successfully step into functions defined in Demand.cs, but whenever I attempt to step into a function that was defined in Role.cs, I get a TargetInvocationException. I should not, however, that this error occurs inside the Role.cs method, so .Net can find the method and execute from it, it just won't show me what it is doing.

How do I step into the methods that are defined in Role.cs, or what would prevent me from viewing that code?

Extra information:

  • Labor.dll is being loaded and called through reflection.
  • When I use reflector on Labor.dll I can view the methods defined in Role.cs
  • The method in Role.cs is throwing an error when it executes code I believe should not be executing, which is why I am more focused on stepping into the code than preventing the TargetInvocationException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-11-01 23:21:52

在摆弄这段代码一段时间后,我相信我找到了错误的原因。

当您使用反射时,.net 似乎会在执行这些方法之前尝试验证方法签名(当您加载包含无效方法调用的方法时,似乎会发生这种情况)。因此,我无法进入 Role.cs,因为 .net 会尝试验证我调用的方法中定义的无效方法签名。通过注释掉无效的方法调用并重新编译 dll,我能够成功地进入 Role.cs 代码。

底线是这样的:

据我所知,通过反射调用的 .dll 中不能有无效的方法签名。当加载使用无效方法的代码时,.net 将发现错误并抛出异常,即使该代码永远不会被执行(在永远不会返回 true 的 if 语句中)。

After fiddling with this code for a while, I believe I found the cause of the error.

It appears that when you're using reflection .net will attempt to validate method signatures before those methods are executed (it appears to occur when you load the method that houses the invalid method call). Therefore, I could not step into Role.cs because .net would attempt to validate an invalid method signature defined in the method I was calling. By commenting out that invalid method call and recompiling the dll, I was able to successfully step into the Role.cs code.

Bottom line is this:

AFAIK, You can not have an invalid method signature within a .dll that you call via reflection. When the code that uses the invalid method is loaded, .net will find the error and throw an exception, even if that code will never be executed (in an if statement that never returns true).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文