Question

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.

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top