1

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

public interface IHasShape
{
    string Shape { get; }
}

public interface IHasColor
{
    string Color { get; }
}

public interface IColoredShape : IHasShape, IHasColor
{
}

然后假设 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);
}

除了作为代理创建的结果之外,没有 IColoredShape 的具体实现。StandardInterceptor 对 IColoredShape 对象进行调用,并将它们委托给适当的“形状”或“颜色”对象。

但是,我一直在使用新的 DynamicProxy2 并且找不到等效的实现。

4

1 回答 1

3

好的,所以如果我对您的理解正确,您有两个具有实现的接口,以及另一个实现这两个接口的接口,并且您想在第三个接口下混合这两个接口的实现,对吗?

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;
}
于 2010-04-17T06:30:52.077 回答