Help Migrating mixins from Castle.DynamicProxy to DynamicProxy2

妖精的绣舞 提交于 2019-12-14 02:33:54

问题


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

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

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.

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


回答1:


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


来源:https://stackoverflow.com/questions/2656663/help-migrating-mixins-from-castle-dynamicproxy-to-dynamicproxy2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!