标签:
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
标签:
原文地址:http://www.cnblogs.com/guofu/p/5260157.html