unity 基于方法注解拦截更改调用处理程序 [英] Unity intercept change call handler based on method annotation

查看:28
本文介绍了unity 基于方法注解拦截更改调用处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有一个方法,我想拦截:

I have a method in a class as follow that I want to intercept:

[CustomTag1(Order = 0)]
[CustomTag2(Order = 1)]
public virtual DoSomething()

如何在使用 CustomAttributeMatchingRule 时将订单值注入 ICallHandler.Order 属性?

How can I inject the order value into ICallHandler.Order property, when using CustomAttributeMatchingRule?

我不希望订单被硬编码到处理程序本身或在注册时.我希望它是方法注释的 Order 属性的变量.

I don't want the order to be hard coded to the handler itself or at registration. I want it to be a variable of the Order property of the method annotation.

推荐答案

我已经使用 HandlerAttribute 实现了这一点 - 一般来说,我无论如何都使用它来在 Unity 中进行属性样式拦截,因为您不必费心创建策略手动 - 而您只需将 HandlerAttribute 应用于您的代码,Unity 将自动为您创建一个策略.

I've achieved this using the HandlerAttribute - in general I use this anyway for attribute-style interception in Unity simply because you don't have to bother creating policies manually - instead you just apply a HandlerAttribute to your code and Unity will automatically create a policy for you.

无论如何,这样的事情可能就是您所追求的.首先像往常一样定义一个调用处理程序,除了参数化它:-

Anyway, something like this is probably what you are after. First define a call handler as usual except parameterise it: -

public class MyCallHandler : ICallHandler
{
    public MyCallHandler(Int32 value)
    {
        Order = value;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("Parameterised call handler!");
        return getNext()(input, getNext);
    }

    public int Order { get; set; }
}

现在不使用 CustomTagAttribute,而是使用 HandlerAttribute:-

Now instead of using the CustomTagAttribute, use a HandlerAttribute:-

public class MyHandler : HandlerAttribute
{
    private readonly Int32 value;
    public MyHandler(Int32 value)
    {
        this.value = value;
    }

    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new MyCallHandler(value);
    }
}

MyHandler 应用于您的类.CreateHandler 方法被调用,此时它会创建一个实例 MyCallHandler:-

The MyHandler is applied on your class. The CreateHandler method gets called, at which point it creates an instance MyCallHandler: -

public class MyClass
{
    [MyHandler(2)] // Order of 2
    public virtual void Foo()
    {
        Console.WriteLine("Inside method!");
    }
}

请注意,我特意将这两个类分开,但实际上您可以只让一个类实现 ICallHandler 接口和 HandlerAttribute 抽象方法(只需返回this").

Note that I've deliberately separated the two classes but in reality you can just have one class implement both the ICallHandler interface and the HandlerAttribute abstract method (just return "this").

如果没有 HandlerAttribute,您可能可以使用您自己的自定义属性来实现类似的功能,但它可以节省您的时间,因为您无需为创建自己的策略而烦恼.

You could probably achieve something similar without the HandlerAttribute, using your own custom attribute, but it saves you time as you don't need to mess around with creating your own policies.

需要注意的一件事 - 如果您采用参数化方法,则不能将调用处理程序作为单例,因为每次都会根据您希望的顺序创建不同的实例.

One thing to note - if you take a parameterised approach, you cannot have your call handlers as singletons, since a different instance will be created every time depending on what order you want it to be in.

这篇关于unity 基于方法注解拦截更改调用处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆