基于方法的注释团结拦截变化调用句柄 [英] Unity intercept change call handler based on method annotation

查看:206
本文介绍了基于方法的注释团结拦截变化调用句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于基于方法的注释团结拦截变化调用句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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