Castle DynamicProxy:代理接口时如何代理等于?
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点棘手。 查看有关代理如何工作的文档。接口代理包装对象并拦截对指定接口的调用。由于
Equals
不是该接口的一部分,因此对 equals 的第二次调用是比较代理,而不是它们的目标。那么第二个
Equals
调用的实现是由什么提供的呢?Proxy 只是实现您的
IDummy
接口的另一个类。与任何类一样,它也有一个基类,这是被调用的Equals
的基本实现。这个基类默认是System.Object
我希望你现在明白这是怎么回事。此问题的解决方案是告诉代理实现一些代理感知基类,该基类会将调用转发到代理目标。它的部分实现可能如下所示:
现在您只需指示代理生成器为您的接口代理使用此基类,而不是默认的。
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 ofEquals
that gets invoked. This base class is by defaultSystem.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:
Now you only need to instruct the proxy generator to use this base class for your interface proxies, instead of the default.
在你的样本中;您的类
Dummy
实现了IDummy
,但还提供了更具体的Equals
重写。 Krzysztof 建议的另一种选择是通过实现IEquatable
将此方法拉入您的界面,例如:这样,您的界面现在包含更具体的
Equals
override,这意味着您生成的代理将根据需要代理对目标的调用。显然,这并不能解决整个问题,并且只会允许您的代理将调用转发到
Equals(IDummy)
而不是Equals(object)
(或GetHashCode< /code> 就此而言)。
In your sample; your class
Dummy
implementsIDummy
, but also provides a more specific override ofEquals
. An alternative to Krzysztof's suggestion is to pull this method into your interface by having it implementIEquatable<T>
, for example: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 notEquals(object)
(orGetHashCode
for that matter).