如何通过IInterceptionBehavior吞掉异常?

发布于 2024-12-14 05:45:00 字数 886 浏览 1 评论 0原文

我有一个像blow这样的IInterceptionBehavior:

public class TraceBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Console.WriteLine(string.Format("Invoke method:{0}",input.MethodBase.ToString()));
        IMethodReturn result = getNext()(input, getNext);
        if (result.Exception == null)
        {
            Console.WriteLine("Invoke successful!");
        }
        else 
        {
            Console.WriteLine(string.Format("Invoke faild, error: {0}", result.Exception.Message));
            result.Exception = null;
        }
        return result;
    }

    public bool WillExecute { get { return true; } }
}

无论我是否将其放在方法上,异常总是抛出。有人可以帮助我吗?

I have a IInterceptionBehavior like blow:

public class TraceBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Console.WriteLine(string.Format("Invoke method:{0}",input.MethodBase.ToString()));
        IMethodReturn result = getNext()(input, getNext);
        if (result.Exception == null)
        {
            Console.WriteLine("Invoke successful!");
        }
        else 
        {
            Console.WriteLine(string.Format("Invoke faild, error: {0}", result.Exception.Message));
            result.Exception = null;
        }
        return result;
    }

    public bool WillExecute { get { return true; } }
}

Regardless of whether I put it upon methods or not, exception throw always. Anyone can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-21 05:45:00

代码看起来不错,但您没有显示如何注册拦截以及如何调用对象。

假设正在调用拦截,那么如果我猜测调用的方法会返回值类型,并且 IMethodReturn.ReturnValue 为 null,这会导致 NullReferenceException。

如果是这种情况,那么返回值类型的默认值也许可以解决您的问题:

public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    Console.WriteLine(string.Format("Invoke method:{0}", input.MethodBase.ToString()));
    IMethodReturn result = getNext()(input, getNext);
    if (result.Exception == null)
    {
        Console.WriteLine("Invoke successful!");
    }
    else
    {
        Console.WriteLine(string.Format("Invoke faild, error: {0}", result.Exception.Message));
        result.Exception = null;

        Type type = ((MethodInfo)input.MethodBase).ReturnType;

        if (type.IsValueType)
        {
            result.ReturnValue = Activator.CreateInstance(type);
        }
    }
    return result;
}

The code looks OK but you haven't shown how the interception is registered and how the object is being called.

Assuming that interception is being called then if I were to guess it would be that that the method invoked returns a value type and IMethodReturn.ReturnValue is null which is causing a NullReferenceException.

If that is the case then perhaps returning the default value for a value type would solve your issue:

public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    Console.WriteLine(string.Format("Invoke method:{0}", input.MethodBase.ToString()));
    IMethodReturn result = getNext()(input, getNext);
    if (result.Exception == null)
    {
        Console.WriteLine("Invoke successful!");
    }
    else
    {
        Console.WriteLine(string.Format("Invoke faild, error: {0}", result.Exception.Message));
        result.Exception = null;

        Type type = ((MethodInfo)input.MethodBase).ReturnType;

        if (type.IsValueType)
        {
            result.ReturnValue = Activator.CreateInstance(type);
        }
    }
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文