单步进入通过反射调用的dll的源代码
我试图通过反射调用 .dll 的方法,但收到 TargetInvocableException
。该场景的工作原理如下:我有一个名为 Labor.dll
的 dll。它包含多个文件,其中包括 Demand.cs
和 Role.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 inRole.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 theTargetInvocationException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在摆弄这段代码一段时间后,我相信我找到了错误的原因。
当您使用反射时,.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 theRole.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).