简体   繁体   English

帮助将mixins从Castle.DynamicProxy迁移到DynamicProxy2

[英]Help Migrating mixins from Castle.DynamicProxy to DynamicProxy2

I am trying to update some code from using DynamicProxy to DynamicProxy2. 我正在尝试从使用DynamicProxy到DynamicProxy2更新一些代码。 In particular we where using DynamicProxy to provide a mixin of two classes. 特别是在使用DynamicProxy提供两个类的混合的地方。 The setup is something like this: 设置是这样的:

public interface IHasShape
{
    string Shape { get; }
}

public interface IHasColor
{
    string Color { get; }
}

public interface IColoredShape : IHasShape, IHasColor
{
}

Then assuming some obvious concrete implementations of IHasShape and IHasColor we would create a mixin like this: 然后假设IHasShape和IHasColor的一些明显的具体实现,我们将创建一个像这样的mixin:

public IColoredShape CreateColoredShapeMixin(IHasShape shape, IHasColor color)
{
    ProxyGenerator gen = new ProxyGenerator();
    StandardInterceptor interceptor = new StandardInterceptor();
    GeneratorContext context = new GeneratorContext();
    context.AddMiniInstance(color);

    return gen.CreateCustomProxy(typeof(IColoredShape), intercetor, shape, context);
}

There are no concrete implementations of IColoredShape except as a result of the proxy creation. 除了作为代理创建的结果之外,没有IColoredShape的具体实现。 The StandardInterceptor takes calls on the IColoredShape object and delegates them to either the 'shape' or 'color' objects as appropriate. StandardInterceptor对IColoredShape对象进行调用,并将它们委托给相应的'shape'或'color'对象。

However, I've been playing around with the new DynamicProxy2 and can't find the equivalent implementation. 但是,我一直在使用新的DynamicProxy2,找不到等效的实现。

OK, so if I understand you correctly you have two interfaces with implementations, and another interfaces that implements both of them and you want to mix the implementations of these two interfaces under the 3rd one, correct? 好的,如果我正确理解了您的实现,则有两个接口,另一个实现这两个接口的接口,您想在第三个接口下混合这两个接口的实现,对吗?

public IColoredShape CreateColoredShapeMixin(IHasShape shape, IHasColor color)
{
    var options = new ProxyGenerationOptions();
    options.AddMixinInstance(shape);
    options.AddMixinInstance(color);
    var proxy = generator.CreateClassProxy(typeof(object), new[] { typeof(IColoredShape ) }, options) as IColoredShape;
    return proxy;
}

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

相关问题 如何从 Castle.DynamicProxy 中的 ProxyGenerationHook 访问自定义方法属性 - How to access custom method attributes from ProxyGenerationHook in Castle.DynamicProxy 向现有的Castle.DynamicProxy代理添加拦截器 - Adding an Interceptor to existing Castle.DynamicProxy proxy 如何在Castle.DynamicProxy中使用IInterceptor? - How use IInterceptor in Castle.DynamicProxy? DynamicProxy2:CreateClassProxyWithTarget + IInterceptor - DynamicProxy2: CreateClassProxyWithTarget + IInterceptor 使用Castle.DynamicProxy和Simple Injector进行基于属性的拦截 - Attribute based interception with Castle.DynamicProxy and Simple Injector 在 C# 中使用 Castle.DynamicProxy 对数组进行排序 - Sort an array using Castle.DynamicProxy in C# 将StructureMap与Castle.DynamicProxy一起使用时是否存在内存泄漏? - Is there a memory leak when using StructureMap with Castle.DynamicProxy? 如何强制Castle.DynamicProxy忽略依赖项的更改版本 - How to force Castle.DynamicProxy to ignore changing versions of dependencies 在Castle.DynamicProxy中,可以在初始化后更改mixin值吗? - In Castle.DynamicProxy is it possible to change a mixin value after initialisation? 如何使用Castle.DynamicProxy拦截基类方法 - How to intercept only base class methods with Castle.DynamicProxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM