[不一样的依赖注入]通过递归实现容器里依赖注入
Posted dotNET跨平台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[不一样的依赖注入]通过递归实现容器里依赖注入相关的知识,希望对你有一定的参考价值。
递归实现依赖注入
创建所需的依赖服务类
1public class Test
2
3 public void PrintTest()
4
5 Console.WriteLine("Hello World");
6
7
8
9public class Test2
10
11 private readonly Test _test;
12
13 public Test2(Test test)
14
15 this._test = test;
16
17 public void PrintTest2()
18
19 _test.PrintTest();
20
21
22
23public class Test3
24
25 private readonly Test2 _test2;
26
27 public Test3(Test2 test2)
28
29 this._test2 = test2;
30
31
32 public void PrintTest3()
33
34 _test2.PrintTest2();
35
36
层层递进之傻瓜式创建实例
1#region 写法一 无脑new 出实例化对象
2var test = new Test();
3var test2 = new Test2(test);
4
5test.PrintTest();
6test2.PrintTest2();
7#endregion
层层递进之反射创建实例
1#region 写法二 反射创建实例对象
2var test = (Test)Activator.CreateInstance(typeof(Test));
3var test2 = (Test2)Activator.CreateInstance(typeof(Test2), test);
4
5test.PrintTest();
6test2.PrintTest2();
7#endregion
层层递进之单层有参构造实例
1#region 写法四 单层有参数构造实例
2using System.Reflection;
3
4var container = new Container();
5container.Add(typeof(Test));
6container.Add<Test2>();
7
8var service = new ContainerService(container);
9var test = service.GenerateService<Test>();
10var test2 = service.GenerateServiceWithParameter<Test2>(); //
11test.PrintTest();
12test2.PrintTest2();
13
14#endregion
层次递进之多层有参构造实例
1#region 多层有参数构造函数实例
2using System.Reflection;
3
4var container = new Container();
5container.Add(typeof(Test));
6container.Add<Test2>();
7container.Add<Test3>();
8
9var service = new ContainerService(container);
10var test = service.GenerateService<Test>();
11var test2 = service.GenerateServiceWithParameter<Test2>();
12var test3 = service.GenerateServiceComplted<Test3>();
13test.PrintTest();
14test2.PrintTest2();
15test3.PrintTest3();
16#endregion
容器类生成以及调用
1public class ContainerService
2
3 private readonly Container _container;
4
5 public ContainerService(Container container)
6
7 this._container = container;
8
9
10 public T GenerateService<T>()
11
12 var type = _container.GetDependencyType(typeof(T));
13 // 实例化
14 return (T)Activator.CreateInstance(type);
15
16
17 /// <summary>
18 /// 单层构造参数实例
19 /// </summary>
20 /// <typeparam name="T"></typeparam>
21 /// <returns></returns>
22 public T GenerateServiceWithParameter<T>()
23
24 var type = _container.GetDependencyType(typeof(T));
25 var constructor = type.GetConstructors().Single();
26 ParameterInfo[] paras = constructor.GetParameters().ToArray();
27 if(paras.Length > 0)
28
29 var arr = new Object[paras.Length];
30 for (int i = 0; i < paras.Length; i++)
31
32 arr[i] = Activator.CreateInstance(paras[i].ParameterType);
33
34 return (T)Activator.CreateInstance(type, arr);
35
36 return (T)Activator.CreateInstance(type);
37
38
39 public Object GenerateType(Type type)
40
41 var serviceType = _container.GetDependencyType(type);
42 var constructor = serviceType.GetConstructors().Single();
43 ParameterInfo[] paras = constructor.GetParameters().ToArray();
44 if (paras.Length > 0)
45
46 var arr = new Object[paras.Length];
47 for (int i = 0; i < paras.Length; i++)
48
49 arr[i] = GenerateType(paras[i].ParameterType);
50
51 return Activator.CreateInstance(serviceType, arr);
52
53 return Activator.CreateInstance(serviceType);
54
55
56 public T GenerateServiceComplted<T>()
57
58 return (T)GenerateType(typeof(T));
59
60
61
62
63public class Container
64
65 List<Type> _lists;
66
67 public Container()
68
69 _lists = new List<Type>();
70
71 public void Add<T>()
72
73 _lists.Add(typeof(T));
74
75
76 public void Add(Type type)
77
78 _lists.Add(type);
79
80
81 public Type GetDependencyType(Type type)
82
83 return _lists.FirstOrDefault(x => x.Name == type.Name);
84
85
以上是关于[不一样的依赖注入]通过递归实现容器里依赖注入的主要内容,如果未能解决你的问题,请参考以下文章