代理目标属性设置为代理属性对应

时间:2016-07-28 15:22:03

标签: c# properties castle proxy-classes castle-dynamicproxy

让我们说下面的代码示例:

public class A 
{
    public virtual string Text { get; set; }
}

我生成A的代理:

A aProxy = ... // Code to generate the whole proxy
aProxy.Text = "hello world";

现在我投射aProxy来获得目标:

IProxyTargetAccessor accessor = (IProxyTargetAccessor)aProxy;
A target = (A)aProxy.DynProxyGetTarget();

我在Text上设置target

target.Text = "bye!";

我无法弄清楚aProxy为什么设置整个属性。它们应该是不同的对象实例,因为一个是包装整个目标的代理,另一个是目标本身。

bool equal = target.Text == aProxy.Text; // TRUE: What's going on?
bool referenceEqual = ReferenceEquals(target, aProxy); // FALSE

0 个答案:

没有答案
相关问题