我正在尝试将一些代码从使用 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 并且找不到等效的实现。