Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle DynamicProxy : How to Proxy Equals when proxying an interface?

I need to use Castle DynamicProxy to proxy an interface by providing an instance of it to ProxyGenerator.CreateInterfaceProxyWithTarget. I also need to make sure that calls to Equals, GetHashCode and ToString hits the methods on the concrete instance, that I am passing, and I can't get that to work.

In other words, I'd like this small sample to print True twice, while in fact it prints True,False:

using System;
using Castle.Core.Interceptor;
using Castle.DynamicProxy;

public interface IDummy
{
    string Name { get; set; }
}

class Dummy : IDummy
{
    public string Name { get; set; }

    public bool Equals(IDummy other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.Name, Name);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as IDummy);
    }      
}

class Program
{
    static void Main(string[] args)
    {
        var g = new ProxyGenerator();
        IDummy first = new Dummy() {Name = "Name"};
        IDummy second = new Dummy() {Name = "Name"};
        IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, new ConsoleLoggerInterceptor());
        IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, new ConsoleLoggerInterceptor());

        Console.WriteLine(first.Equals(second));         
        Console.WriteLine(firstProxy.Equals(secondProxy));
    }
}

internal class ConsoleLoggerInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("Invoked " + invocation.Method.Name);
    }
}

Is this possible with DynamicProxy ? How ?

like image 302
driis Avatar asked Jun 03 '10 19:06

driis


1 Answers

This is a bit tricky. Take a look at documentation on how proxies work. Interface proxies wrap an object and intercept calls to designated interface(s). Since Equals is not part of that interface the second call to equals is comparing proxies, not their targets.

So what provides the implementation for the second Equals call?

Proxy is just another class implementing your IDummy interface. As any class it also has a base class, and that's the base implementation of Equals that gets invoked. This base class is by default System.Object

I hope you see now where this is going. Solution to this problem is to tell proxy to implement some proxy aware base class that will forward the calls to proxy target. Part of its implementation might look like this:

public class ProxyBase
{
    public override bool Equals(object obj)
    {
        var proxy = this as IProxyTargetAccessor;
        if (proxy == null)
        {
            return base.Equals(obj);
        }
        var target = proxy.DynProxyGetTarget();
        if (target == null)
        {
            return base.Equals(obj);
        }
        return target.Equals(obj);
    }
    // same for GetHashCode
}

Now you only need to instruct the proxy generator to use this base class for your interface proxies, instead of the default.

var o = new ProxyGenerationOptions();
o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, o);
IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, o);
like image 157
Krzysztof Kozmic Avatar answered Sep 20 '22 03:09

Krzysztof Kozmic