Question: I've found a method using reflection (and got it's `MethodInfo`). How can I invoke it without getting `TargetInvocationException` when exceptions are thrown?

**Update**

I'm creating a command implementation where the commands are handled by classes which implemement

```
public interface ICommandHandler<T> where T : class, ICommand
{
    public void Invoke(T command);
}

```

Since there is one dispatcher which takes care of find and map all handlers to the correct command I can't invoke the methods directly but by using reflection. Something like:

```
var handlerType = tyepof(IHandlerOf<>).MakeGenericType(command.GetType());
var method = handlerType.GetMethod("Invoke", new [] { command.GetType() });
method.Invoke(theHandler, new object[]{command});

```

It works fine, but I want all exceptions to get passed on to the code that invoked the command.

So that the caller can use:

```
try
{
    _dispatcher.Invoke(new CreateUser("Jonas", "Gauffin"));
}
catch (SomeSpecificException err)
{
    //handle it.
}

```

Instead of having to catch `TargetInvocationException`.

(I know that I can throw the inner exception, but that's pretty worthless since the stack trace is destroyed)

**Update2**

[Here](http://timwi.blogspot.se/2010/03/dynamic-method-invocation-without.html) is a possible solution..

But it seems more like a hack. Aren't there a better solution? Maybe with expressions or something?

Answer: 
You can't. That's the specified way that exceptions are propagated by invoking a method via reflection. You can always catch `TargetInvocationException` and then throw the "inner" exception obtained via the [`InnerException`](http://msdn.microsoft.com/en-us/library/system.exception.innerexception.aspx) property, if you want the effect to be the original exception being thrown.

(You'll lose the original stack trace, mind you. It's possible that there's a way to prevent that, but it's tricky. I believe there *may* be more support for this in .NET 4.5; I'm not sure.)
You can call Invoke on the mehtodinfo instance, but the first argument of the call is the target (The object that the method info belongs to). If you pass this and it has access to call it, you should not get the exception.