[英]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.