MethodInfo.Invoke and TargetInvocationException



We have a base class that relies heavily on MethodInfo.Invoke.

Often, as such things happen, exceptions are thrown in the method that was invoked, and this pops up as a TargetInvocationException, at the place of the Invoke command.

I have tried wrapping my call to Invoke like this:

try
{
mi.Invoke(...);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}

However, this changes the stack trace. Is there a way for me to re-throw the inner exception so that Visual Studio pops up at the method being invoked with the exception that was thrown there, instead of always stopping in our base class?

I am working on replacing the calls to Invoke with delegates, but it's a lot of code so it's neither quick going nor bug-free so I was hoping there was a temporary fix for this while I rework the code.

--
Lasse Vågsæther Karlsen
mailto:lasse@xxxxxxxxxxx
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
.



Relevant Pages

  • Re: How to make multiple datagrid rows invisible??
    ... An InvalidOperationException exception is thrown in cases when the ... failure to invoke a method is caused by a reason other than an invalid ... ChrisM schreef: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Show dialog in response to message
    ... misunderstanding something about Invoke, because it didn't work. ... What line of code in that example throws the exception? ... might interact with another of your GUI components, ... you should probably use BeginInvoke(), because you wrote that the code is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Worth an RCR? static_type_check, polymorphic_type_check, quacks_like
    ... Keep in mind, though, that static type checking isn't just some safety ... net that Matz forgot to include in Ruby. ... Only much later when you invoke the methods on the object do ... class TypeCheckException < Exception ...
    (comp.lang.ruby)
  • how to get VerificationException to be thrown without actually invoking the method
    ... There are two exceptions that I know of that can be thrown if a dynamic ... method was created with invalid IL: ... I would like to manually invoke this ... I need to catch this verification exception and swap the bad ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Deserializing a VB file into IDesignerHost
    ... I can not give very useful hint to this exception. ... CodeDomSerializerBase ... invoke corresponding child ... RootCodeDomSerializer.Deserialize requires we pass CodeTypeReference type ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)