Ninject条件绑定(转载)
public interface ILog { void Put(string value); } class RedImpl : ILog { public void Put(string value) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(value); } } class BlueImpl : ILog { public void Put(string value) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine(value); } } [Red] class ConsumerA { public ConsumerA(ILog service) { service.Put("Red"); } } class ConsumerB { public ConsumerB( ILog service) { service.Put("Blue"); } } public class RedAttribute : Attribute { } public class BlueAttribute : Attribute { } class Program { static void Main(string[] args) { var kernel = new StandardKernel(); kernel.Bind<ILog>().To<RedImpl>().WhenClassHas <RedAttribute>(); kernel.Bind<ILog>().To<BlueImpl>().WhenTargetHas<BlueAttribute>(); var log = kernel.Get<ILog>(); var cA = new ConsumerA(log); Console.Read(); } }
调试出错,以后需要使用的时候再说吧。
http://www.cnblogs.com/willick/p/3223042.html