帮助将mixins从Castle.DynamicProxy迁移到DynamicProxy2 [英] Help Migrating mixins from Castle.DynamicProxy to DynamicProxy2

查看:158
本文介绍了帮助将mixins从Castle.DynamicProxy迁移到DynamicProxy2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用DynamicProxy到DynamicProxy2更新一些代码.特别是我们在哪里使用DynamicProxy提供两个类的混合.设置是这样的:

I am trying to update some code from using DynamicProxy to DynamicProxy2. In particular we where using DynamicProxy to provide a mixin of two classes. The setup is something like this:

public interface IHasShape
{
    string Shape { get; }
}

public interface IHasColor
{
    string Color { get; }
}

public interface IColoredShape : IHasShape, IHasColor
{
}

然后假设IHasShape和IHasColor的一些明显的具体实现,我们将创建一个像这样的mixin:

Then assuming some obvious concrete implementations of IHasShape and IHasColor we would create a mixin like this:

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);
}

除了作为代理创建的结果之外,没有IColoredShape的具体实现. StandardInterceptor对IColoredShape对象进行调用,并根据需要将它们委托给'shape'或'color'对象.

There are no concrete implementations of IColoredShape except as a result of the proxy creation. The StandardInterceptor takes calls on the IColoredShape object and delegates them to either the 'shape' or 'color' objects as appropriate.

但是,我一直在使用新的DynamicProxy2,却找不到等效的实现.

However, I've been playing around with the new DynamicProxy2 and can't find the equivalent implementation.

推荐答案

好,因此,如果我正确理解了您有两个实现的接口,另一个实现了这两个接口的接口,并且您想混合使用这两个实现接口在第三个之下,对吗?

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;
}

这篇关于帮助将mixins从Castle.DynamicProxy迁移到DynamicProxy2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆