简体   繁体   English

Castle Dynamic Proxy如何在CreateClassProxyWithTarget中合并数据

[英]Castle Dynamic Proxy how to merge data in CreateClassProxyWithTarget

It is possible to add proxy capabilities to an already generated and not empty object using Castle Dynamic Proxy? 是否可以使用Castle Dynamic Proxy向已经生成的非空对象添加代理功能?

I've tried this: 我已经试过了:

Dog _myDog=new Dog();
_myDog.Name="Fuffy";

var _proxyDog = generator.CreateClassProxyWithTarget<Dog>(_myDog, ProxyGenerationOptions.Default, new DogInterceptor());

_proxyDog results as a new object. _proxyDog结果为新对象。

Now this is only an example, in real world application my object has 30+ properties and I want to know if I can avoid to copy those props one by one! 现在这只是一个示例,在现实世界的应用程序中,我的对象具有30多个属性,我想知道是否可以避免一个一个地复制这些道具!

Yes it is. 是的。 The only problem: ProxyGenerator needs to instantiate an object of that type anyway. 唯一的问题:ProxyGenerator无论如何都需要实例化该类型的对象。 This code is actually working correctly in my project: 该代码实际上在我的项目中正常工作:

public static class MongoExtensions
{
    static readonly ProxyGenerator pg = new ProxyGenerator();
    public static MongoCollection GetRetryCollection(this MongoDatabase db, string collectionName, int retryCount = 5, int pauseBetweenRetries = 2000)
    {
        var coll = db.GetCollection(collectionName);
        return (MongoCollection)pg.CreateClassProxyWithTarget(typeof(MongoCollection), coll, new object[] { db, collectionName, coll.Settings }, new RetryingInterceptor { RetryCount = retryCount, PauseBetweenCalls = pauseBetweenRetries });
    }
}

Paramerts of CreateClassProxyWithTarget are: CreateClassProxyWithTarget的参数为:

  • type of the proxied object, 代理对象的类型,
  • proxied instance 代理实例
  • array of constructor paramers for the proxied type. 代理类型的构造函数参数数组。
  • interceptor for this proxy. 此代理的拦截器。

I can't really explain, why it need constructor parameters for the object, but this code work correctly for me. 我真的无法解释为什么它需要该对象的构造函数参数,但是这段代码对我来说是正确的。

I had the same issue so using vlad's suggestion this worked for me: 我遇到了同样的问题,因此使用弗拉德的建议对我有用:

var _proxyDog = generator.CreateClassProxyWithTarget(_myDog.GetType(), _myDog, new DogInterceptor());

From what I can see a new wrapper (proxy) is created that simulates the real class and the wrapped class (target) is my original object. 从我看到的内容中,创建了一个模拟真实类的新包装器(代理),而包装的类(目标)是我的原始对象。

additional: nope i checked again and the 'target type' is correct but the proxy isn't reflecting the values set in it. 另外:不,我再次检查,“目标类型”正确,但是代理未反映其中设置的值。 i'd consider this a bug; 我认为这是一个错误; and a big one. 还有一大堆

from the first image you can see the original class with all the imports satisfied. 从第一个图像中,您可以看到满足所有导入条件的原始类。

image here... sadly i can't post the images for you to look at as it appears i need a reputation of '10'; 图片在这里...可悲的是,我无法发布图片供您查看,因为看起来我的信誉为'10'; apparently. 显然。 how silly is that? 那有多傻? sorry. 抱歉。

as you can see the properties on the proxy are both null and incomplete but the original class under '_target' is still intact. 如您所见,代理上的属性均为null和不完整,但“ _target”下的原始类仍然完整。 probing the proxies properties results in aberrant behaviour and you shouldn't need to inspect the target as you would be obviating the purpose of the decorator. 探测代理属性会导致异常行为,并且您不需要检查目标,因为这将消除装饰器的作用。

another image here... 另一张图片...

i would expect either all the properties to be there mimicking in full the underlying class; 我希望所有的属性都可以完全模仿基础类。 or none of them and the mapping to be dynamic. 或没有一个,并且映射是动态的。 as it stands it isn't working for me either, as the one property i actually want to gain access to is being exposed with an incorrect value. 就目前而言,它对我也不起作用,因为我实际上想要访问的一个属性暴露了一个不正确的值。

Colin. 科林

The approach in the original question is fine. 原始问题中的方法很好。 However, you need to ensure that all of the properties in the class being wrapped are marked as virtual. 但是,您需要确保将要包装的类中的所有属性都标记为虚拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM