Stepping into the source of a dll being called through reflection
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 开发者_JS百科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
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).
精彩评论