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

posted @ 2015-06-23 18:05  江境纣州  阅读(139)  评论(0编辑  收藏  举报