簡體   English   中英

通過反射調用方法而不會獲得TargetInvocationException

[英]Invoke a method through reflection without getting TargetInvocationException

我找到了一個使用反射的方法(並得到了它的MethodInfo )。 拋出異常時,如何在不獲取TargetInvocationException情況下調用它?

更新

我正在創建一個命令實現,其中命令由實現的類處理

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

由於有一個調度程序負責查找並將所有處理程序映射到正確的命令,因此我不能直接調用方法,而是使用反射。 就像是:

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

它工作正常,但我希望所有異常都傳遞給調用該命令的代碼。

這樣調用者就可以使用:

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

而不是必須捕獲TargetInvocationException

(我知道我可以拋出內部異常,但由於堆棧跟蹤被破壞,這是非常沒用的)

UPDATE2

是一個可能的解決方案..

但它似乎更像是一個黑客。 有沒有更好的解決方案? 也許用表情或什么?

創建一個DelegateMethodInfo (通過的重載之一Delegate.CreateDelegate )和調用該代替。 不會將方法拋出的任何異常包裝在類似MethodInfo.InvokeTargetInvocationException中。

class Foo
{
    static void ThrowingMethod()
    {
        throw new NotImplementedException();
    }

    static MethodInfo GetMethodInfo()
    {
        return typeof(Foo)
                .GetMethod("ThrowingMethod", BindingFlags.NonPublic | BindingFlags.Static);
    }

    // Will throw a NotImplementedException
    static void DelegateWay()
    {
        Action action = (Action)Delegate.CreateDelegate
                                    (typeof(Action), GetMethodInfo());
        action();
    }

    // Will throw a TargetInvocationException 
    // wrapping a NotImplementedException
    static void MethodInfoWay()
    {
        GetMethodInfo().Invoke(null, null);
    }
}

編輯

(正如OP指出的那樣,DynamicInvoke不會在這里工作,因為它也包裝了)

根據您的更新,我會使用dynamic

((dynamic)theHandler).Invoke(command);

你不能。 這是通過反射調用方法傳播異常的指定方式。 如果希望效果是拋出的原始異常,則可以始終捕獲TargetInvocationException ,然后拋出通過InnerException屬性獲取的“內部”異常。

(請注意,您將丟失原始堆棧跟蹤。可能有一種方法可以防止這種情況,但這很棘手。我相信在.NET 4.5中可能會有更多支持;我不確定。)

您可以在mehtodinfo實例上調用Invoke,但調用的第一個參數是目標(方法信息所屬的對象)。 如果你通過它並且有權調用它,你就不應該得到例外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM