- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我需要使用 CaSTLe DynamicProxy 来代理接口(interface),方法是向 ProxyGenerator.CreateInterfaceProxyWithTarget 提供接口(interface)实例。我还需要确保对 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 可以吗?怎么样?
最佳答案
这有点棘手。 Take a look at documentation关于代理如何工作。接口(interface)代理包装一个对象并拦截对指定接口(interface)的调用。由于 Equals
不是该接口(interface)的一部分,因此对 equals 的第二次调用是比较代理,而不是它们的目标。
那么是什么为第二个 Equals
调用提供了实现?
Proxy 只是实现您的IDummy
接口(interface)的另一个类。与任何类一样,它也有一个基类,这是被调用的 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
}
现在您只需要指示代理生成器将此基类用于您的接口(interface)代理,而不是默认值。
var o = new ProxyGenerationOptions();
o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, o);
IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, o);
关于c# - 城堡动态代理 : How to Proxy Equals when proxying an interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2969274/
关于我的 MVP 实现的快速问题: 目前我有下面的代码,其中 presenter 和 view 都是通过容器解析的。 然后演示者调用 View.Init 将自己传递给 View 。 但是我想知道是否有
我正在尝试在 NHibernate 上用 CaSTLe 做一个简单的“从产品中选择计数(*),日期 > xxx”。 如果我直接使用 NHibernate,我可以重用 this question答案但不
刚接触城堡/温莎,请耐心等待。 我目前正在使用框架System.Web.Mvc.Extensibility在其启动代码中,它注册了 HttpContextBase,如下所示: container.Re
您好,我在尝试初始化 ActiveRecord 时遇到异常,我不知道我遗漏了什么。我正试图说服我工作的公司使用 CaSTLe ActiveRecord,如果我不能演示它是如何工作的,它看起来不会很好。
对于我当前的项目,我在 C# 中使用 CaSTLe 的 ActiveRecord。对于我的一张表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度的转换)。为了保持我的代码干净,我喜欢在对象映射类
在 IIS 中托管的 Windsor ioc、wcf 设施设置下,raven doc session 和存储的推荐生活方式是什么? 我一直看到这个错误: Error TempPathInUse (JE
我决定将我的项目 ASP.NET MVC 4 中的 CaSTLe ActiveRecord 版本从 2.1.2 版本迁移到 3.0.0。还更新了 NHibernate 2.1.2 版本到 3.1.0
我是一名优秀的程序员,十分优秀!