zoukankan      html  css  js  c++  java
  • 在Ninject 向构造参数中注入具有相同类型的参数

    实际上这个有多种解决方法,加自定义Attribute,或Named(),但这些方式有一些侵入性,Named,要引用Ninject, 自定义Attribute,还要还要再写几行代码吗,所以使用下面的方法,

     

        public class All
        {
            private readonly II _a;
            private readonly II _b;
     
            public All( II a, II b)
            {
                _a = a;
                _b = b;
            }
     
     
            public void Print()
            {
                Console.WriteLine(_a.get());
     
                Console.WriteLine(_b.get());
            }
        }
     
     
        public interface II
        {
            string get();
        }
     
        class A : II
        {
            public string get()
            {
                return "a";
            }
        }
     
        class B : II
        {
            public string get()
            {
                return "b";
            }
        }

     

       1:   [TestClass]
       2:      public class UnitTest1
       3:      {
       4:          [TestMethod]
       5:          public void TestMethod1()
       6:          {
       7:              var ker = new Ninject.StandardKernel();
       8:   
       9:              //ker.Bind<II>().To<A>().Named("a");
      10:              //ker.Bind<II>().To<A>().When(x=>x);
      11:              ker.Bind<II>().To<A>().When(x => x.Target.Name == "a");
      12:   
      13:              ker.Bind<II>().To<B>().When(x => x.Target.Name == "b");
      14:   
      15:              //ker.Bind<All>().To<All>().WithConstructorArgument("a", new A()).WithConstructorArgument("b", new B());
      16:   
      17:              var all = ker.Get<All>();
      18:   
      19:              all.Print();
      20:          }
      21:      }
  • 相关阅读:
    SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤
    svn branch and merge(svn切换分支和合并)详解
    WPF 后台任务 等待动画 样例 && C# BackgroundWorker 详解
    WPF CheckBox 滑块 样式 开关
    WPF自适应可关闭的TabControl 类似浏览器的标签页
    Bootstrap WPF Style(二)--Glyphicons 字体图标
    WPF 中的 Pack URI地(资源文件加载)
    Bootstrap WPF Style,Bootstrap风格的WPF样式
    tomcat修改server.xml的虚拟目录,启动eclipse后清空
    js修改css属性值
  • 原文地址:https://www.cnblogs.com/zbw911/p/3173512.html
Copyright © 2011-2022 走看看