Castle DynamicProxy:代理接口时如何代理等于?

发布于 2024-09-03 18:03:47 字数 1518 浏览 10 评论 0原文

我需要使用 Castle DynamicProxy 通过向 ProxyGenerator.CreateInterfaceProxyWithTarget 提供接口实例来代理接口。我还需要确保对 Equals、GetHashCode 和 ToString 的调用会命中我正在传递的具体实例上的方法,但我无法使其正常工作。

换句话说,我希望这个小样本打印 True 两次,而实际上它打印 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);
    }
}

这对于 DynamicProxy 可能吗?如何 ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-09-10 18:03:47

这有点棘手。 查看有关代理如何工作的文档。接口代理包装对象并拦截对指定接口的调用。由于 Equals 不是该接口的一部分,因此对 equals 的第二次调用是比较代理,而不是它们的目标。

那么第二个 Equals 调用的实现是由什么提供的呢?

Proxy 只是实现您的 IDummy 接口的另一个类。与任何类一样,它也有一个基类,这是被调用的 Equals 的基本实现。这个基类默认是System.Object

我希望你现在明白这是怎么回事。此问题的解决方案是告诉代理实现一些代理感知基类,该基类会将调用转发到代理目标。它的部分实现可能如下所示:

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
}

现在您只需指示代理生成器为您的接口代理使用此基类,而不是默认的。

var o = new ProxyGenerationOptions();
o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, o);
IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, o);

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);
聊慰 2024-09-10 18:03:47

在你的样本中;您的类 Dummy 实现了 IDummy,但还提供了更具体的 Equals 重写。 Krzysztof 建议的另一种选择是通过实现 IEquatable 将此方法拉入您的界面,例如:

public interface IDummy : IEquatable<IDummy>
{
    string Name { get; set; }
}

这样,您的界面现在包含更具体的 Equals override,这意味着您生成的代理将根据需要代理对目标的调用。

显然,这并不能解决整个问题,并且只会允许您的代理将调用转发到 Equals(IDummy) 而不是 Equals(object) (或 GetHashCode< /code> 就此而言)。

In your sample; your class Dummy implements IDummy, but also provides a more specific override of Equals. An alternative to Krzysztof's suggestion is to pull this method into your interface by having it implement IEquatable<T>, for example:

public interface IDummy : IEquatable<IDummy>
{
    string Name { get; set; }
}

That way, your interface now includes the more specific Equals override, which means your generated proxy will proxy calls to your target as required.

Obviously this doesn't solve the entire problem and will only allow your proxy to forward calls to Equals(IDummy) and not Equals(object) (or GetHashCode for that matter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文