1

Let's say I have a type like this:

public class Foo
{
    public virtual void InterceptedByA() { }

    public virtual void InterceptedByB() { }
}

I have two selectors named InterceptorA and InterceptorB. I want to use multiple IProxyGenerationHook implementations to ensure they only intercept their own methods. ProxyGenerator class accepts an array of interceptors but I can only use single IProxyGenerationHook instance in ProxyGenerationOptions constructor:

var options = new ProxyGenerationOptions(new ProxyGenerationHookForA());

Is there a way to use multiple IProxyGenerationHook implementations for creating a proxy?

1 Answer 1

1

The IProxyGenerationHook is used at proxy-ing time only once for the object being proxy-ed. If you want fine-grained control about which interceptors are used for a method you should use the IInterceptorSelector

Here is a (very stupid) example that could help you see how you could use the IInterceptorSelector to match the methods called. Of course you wouldn't rely on the method names in order to match the selectors but this is left as an exercice

internal class Program
{
    private static void Main(string[] args)
    {
        var pg = new ProxyGenerator();

        var options = new ProxyGenerationOptions();
        options.Selector = new Selector();
        var test = pg.CreateClassProxy<Foo>(options, new InterceptorA(), new InterceptorB());

        test.InterceptedByA();
        test.InterceptedByB();
    }
}

public class Foo
{
    public virtual void InterceptedByA() { Console.WriteLine("A"); }
    public virtual void InterceptedByB() { Console.WriteLine("B"); }
}


public class Selector : IInterceptorSelector
{
    public IInterceptor[] SelectInterceptors(Type type, System.Reflection.MethodInfo method, IInterceptor[] interceptors)
    {
        return interceptors.Where(s => s.GetType().Name.Replace("or", "edBy") == method.Name).ToArray();
    }
}

public class InterceptorA : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("InterceptorA");
        invocation.Proceed();
    }
}
public class InterceptorB : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("InterceptorB");
        invocation.Proceed();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.