我想在我的代码库中允许声明式 mixin 管理。我想声明一个接口
public interface IMyRepo : IRepository, ICanFindPeopleByName, ICantSing {}
所以我的类只能消耗他们需要的数据访问层的位。在我的 IoC 容器中,我想将这些接口的实现聚合到一个实例中。但是,当我执行与引用线程类似的操作时,生成器会抛出一个异常,指出接口在多个地方实现。除了实现自己的拦截器并通过之外,我还能做什么?
相关主题:
更好的例子(代码墙)
public interface IIceCream {
void Eat();
}
public class IceCream : IIceCream {
public void Eat() { Console.WriteLine("Yummy!"); }
}
public interface ICake {
void NomNom();
}
public class Cake : ICake {
public void NomNom() { Console.WriteLine("Cakey!"); }
}
public interface ISprinkles {
void Oogle();
}
public class Sprinkles : ISprinkles {
public void Oogle(){ Console.WriteLine("Its Pretty!");}
}
public interface IIceCreamWithCakeAndSprinkles : IIceCream, ICake, ISprinkles {}
public class Program
{
public static void Main()
{
var gen = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new IceCream());
options.AddMixinInstance(new Cake());
options.AddMixinInstance(new Sprinkles());
var result =
gen.CreateClassProxy(typeof (object), new[] {typeof (IIceCreamWithCakeAndSprinkles)}, options) as IIceCreamWithCakeAndSprinkles;
}
}
投掷
InvalidMixinConfigurationException: "The mixin IceCream adds the interface 'ConsoleApplication1.IIceCream' to the generated proxy, but the interface already exists in the proxy's additional interfaces. A mixin cannot add an interface already implemented in another way."