zoukankan      html  css  js  c++  java
  • Resolve Type中构造函数传值

    1

     1   class Test
     2     {
     3 
     4         private ITeacher _teacher;
     5         private IStudent _student;
     6         public Test(ITeacher tea, IStudent stu)
     7         {
     8             _teacher = tea;
     9             _student = stu;
    10         }
    11     }

    2

     1    class Student:IStudent
     2     {
     3 
     4         public Student()
     5         {
     6             int i = 1;
     7         }
     8 
     9 
    10         public int Age
    11         {
    12             get { throw new NotImplementedException(); }
    13         }
    14     }

    3

     1    class Teacher:ITeacher
     2     {
     3 
     4         public Teacher()
     5         {
     6             int m = 0;
     7         }
     8         public int Age
     9         {
    10             get { throw new NotImplementedException(); }
    11         }
    12     }

    4

    1             _container.RegisterType<IStudent, Student>(new ContainerControlledLifetimeManager());
    2             _container.RegisterType<ITeacher, Teacher>(new ContainerControlledLifetimeManager());
    3 
    4             var viewModelTypeName = string.Format(CultureInfo.InvariantCulture, "App5.Test, App5, Version=1.0.0.0, Culture=neutral");
    5             var viewModelType = Type.GetType(viewModelTypeName);
    6 
    7             _container.Resolve(viewModelType);

    并没有注册过Test类

    但是Resolve Test类的时候,Test构造函数中的参数会自动Resolve

    即相当于调用Test构造函数前,会进行如下操作

    1 _container.Resolve(ISutdent);

    2 _container.Resolve(ITeacher);  

    并将结果作为Test的构造函数参数传给Test

    注意:如果Test中构造函数所传参数并没有通过注册,Test类的Resolve操作会报错

    关于RegeistType可以查看http://www.cnblogs.com/50614090/archive/2012/02/01/2334866.html

  • 相关阅读:
    shopping car 1.0
    文件分类
    求1-100的所有数的和
    输出 1-100 内的所有奇数和
    求1-2+3-4+5 ... 99的所有数的和
    关闭提示的下拉框
    h5页面乱码-设置编码
    常用的css
    渲染后新元素没有绑定事件
    爬虫日记-关于一些动态爬取
  • 原文地址:https://www.cnblogs.com/guofu/p/5260157.html
Copyright © 2011-2022 走看看