Unity intercept call handler changes based on method annotation

I have a method in the class, as follows, what I want to intercept:

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

How can I enter an order value in a property ICallHandler.Orderwhen used CustomAttributeMatchingRule?

I do not want the order to be hardcoded to the processor itself or during registration. I want this to be a variable of the Order property of the method annotation.

+2
source share
1 answer

HandlerAttribute. , Unity , - HandlerAttribute , Unity .

, , , . , , : -

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: -

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: -

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

, , ICallHandler, HandlerAttribute ( "this").

, - HandlerAttribute, , , .

, , , , .

+5

All Articles