InjectModule.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace IFramework.Inject
  6. {
  7. /// <summary>
  8. /// 注入模块
  9. /// </summary>
  10. public partial class InjectModule : Module, IInjectModule
  11. {
  12. private InjectTypeMap _type;
  13. private InjectInstanceMap _instance;
  14. #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
  15. protected override ModulePriority OnGetDefautPriority()
  16. {
  17. return ModulePriority.Config;
  18. }
  19. protected override void Awake()
  20. {
  21. _type = new InjectTypeMap();
  22. _instance = new InjectInstanceMap();
  23. }
  24. protected override void OnDispose()
  25. {
  26. this.Clear();
  27. }
  28. #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
  29. /// <summary>
  30. /// 清除
  31. /// </summary>
  32. public void Clear()
  33. {
  34. this._instance.Clear();
  35. this._type.Clear();
  36. }
  37. /// <summary>
  38. /// 反射注入
  39. /// </summary>
  40. /// <param name="obj"></param>
  41. public void Inject(object obj)
  42. {
  43. if (obj == null)
  44. {
  45. return;
  46. }
  47. MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  48. for (int i = members.Length - 1; i >= 0; i--)
  49. {
  50. MemberInfo member = members[i];
  51. if (member.IsDefined(typeof(InjectAttribute), true))
  52. {
  53. InjectAttribute attr = member.GetCustomAttributes(typeof(InjectAttribute), true)[0] as InjectAttribute;
  54. if (member is PropertyInfo)
  55. {
  56. PropertyInfo propertyInfo = member as PropertyInfo;
  57. propertyInfo.SetValue(obj, this.GetValue(propertyInfo.PropertyType, attr.name, new object[0]), null);
  58. }
  59. else if (member is FieldInfo)
  60. {
  61. FieldInfo fieldInfo = member as FieldInfo;
  62. fieldInfo.SetValue(obj, this.GetValue(fieldInfo.FieldType, attr.name, new object[0]));
  63. }
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 注入所有
  69. /// </summary>
  70. public void InjectInstances()
  71. {
  72. foreach (object instance in this._instance.Values)
  73. {
  74. this.Inject(instance);
  75. }
  76. }
  77. /// <summary>
  78. /// 注册
  79. /// </summary>
  80. /// <typeparam name="Type"></typeparam>
  81. /// <param name="name"></param>
  82. public void Subscribe<Type>(string name = null)
  83. {
  84. this.Subscribe(typeof(Type), typeof(Type), name);
  85. }
  86. /// <summary>
  87. /// 注册
  88. /// </summary>
  89. /// <typeparam name="BaseType"></typeparam>
  90. /// <typeparam name="Type"></typeparam>
  91. /// <param name="name"></param>
  92. public void Subscribe<BaseType, Type>(string name = null) where Type : BaseType
  93. {
  94. this.Subscribe(typeof(BaseType), typeof(Type), name);
  95. }
  96. /// <summary>
  97. /// 注册
  98. /// </summary>
  99. /// <param name="source"></param>
  100. /// <param name="target"></param>
  101. /// <param name="name"></param>
  102. public void Subscribe(Type source, Type target, string name = null)
  103. {
  104. this._type.Set(source, name, target);
  105. }
  106. /// <summary>
  107. /// 注册实例
  108. /// </summary>
  109. /// <typeparam name="Type"></typeparam>
  110. /// <param name="instance"></param>
  111. /// <param name="name"></param>
  112. /// <param name="inject"></param>
  113. public void SubscribeInstance<Type>(Type instance, string name, bool inject = true) where Type : class
  114. {
  115. this.SubscribeInstance(typeof(Type), instance, name, inject);
  116. }
  117. /// <summary>
  118. /// 注册实例
  119. /// </summary>
  120. /// <param name="baseType"></param>
  121. /// <param name="instance"></param>
  122. /// <param name="name"></param>
  123. /// <param name="inject"></param>
  124. /// <exception cref="Exception"></exception>
  125. public void SubscribeInstance(Type baseType, object instance, string name, bool inject = true)
  126. {
  127. Type type = instance.GetType();
  128. if (type != baseType && !type.IsExtendInterface(baseType) && !type.IsSubclassOf(baseType))
  129. {
  130. throw new Exception(string.Format("{0} is Not {1}", type, baseType));
  131. }
  132. this._instance.Set(baseType, name, instance);
  133. if (inject)
  134. {
  135. this.Inject(instance);
  136. }
  137. }
  138. /// <summary>
  139. /// 获取
  140. /// </summary>
  141. /// <typeparam name="T"></typeparam>
  142. /// <param name="name"></param>
  143. /// <param name="args"></param>
  144. /// <returns></returns>
  145. public T GetValue<T>(string name = null, params object[] args) where T : class
  146. {
  147. return (T)GetValue(typeof(T), name, args);
  148. }
  149. /// <summary>
  150. /// 获取
  151. /// </summary>
  152. /// <param name="baseType"></param>
  153. /// <param name="name"></param>
  154. /// <param name="constructorArgs"></param>
  155. /// <returns></returns>
  156. public object GetValue(Type baseType, string name = null, params object[] constructorArgs)
  157. {
  158. object item = _instance.Get(baseType, name);
  159. if (item != null) return item;
  160. Type map = this._type.Get(baseType, name);
  161. if (map != null)
  162. {
  163. return this.CreateInstance(map, constructorArgs);
  164. }
  165. return null;
  166. }
  167. /// <summary>
  168. /// 获取
  169. /// </summary>
  170. /// <param name="type"></param>
  171. /// <returns></returns>
  172. public IEnumerable<object> GetValues(Type type)
  173. {
  174. var ie = _instance.GetInstances(type); ;
  175. foreach (var item in ie)
  176. {
  177. yield return item;
  178. }
  179. var ies = _type.GetTypes(type);
  180. foreach (var item in ies)
  181. {
  182. object obj = Activator.CreateInstance(item);
  183. this.Inject(obj);
  184. yield return obj;
  185. }
  186. }
  187. /// <summary>
  188. /// 获取
  189. /// </summary>
  190. /// <typeparam name="Type"></typeparam>
  191. /// <returns></returns>
  192. public IEnumerable<Type> GetValues<Type>()
  193. {
  194. foreach (object obj in this.GetValues(typeof(Type)))
  195. {
  196. yield return (Type)((object)obj);
  197. }
  198. }
  199. private object CreateInstance(Type type, params object[] ctrArgs)
  200. {
  201. if (ctrArgs != null && ctrArgs.Length != 0)
  202. {
  203. object obj2 = Activator.CreateInstance(type, ctrArgs);
  204. this.Inject(obj2);
  205. return obj2;
  206. }
  207. ConstructorInfo[] ctrs = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
  208. if (ctrs.Length < 1)
  209. {
  210. object obj3 = Activator.CreateInstance(type);
  211. this.Inject(obj3);
  212. return obj3;
  213. }
  214. ParameterInfo[] maxParameters = ctrs[0].GetParameters();
  215. for (int i = ctrs.Length - 1; i >= 0; i--)
  216. {
  217. ParameterInfo[] parameters = ctrs[i].GetParameters();
  218. if (parameters.Length > maxParameters.Length)
  219. {
  220. maxParameters = parameters;
  221. }
  222. }
  223. object[] args = maxParameters.Select(delegate (ParameterInfo p)
  224. {
  225. if (p.ParameterType.IsArray)
  226. {
  227. return this.GetValues(p.ParameterType);
  228. }
  229. object tmp = this.GetValue(p.ParameterType, null, new object[0]);
  230. if (tmp != null)
  231. {
  232. return tmp;
  233. }
  234. return this.GetValue(p.ParameterType, p.Name, new object[0]);
  235. }).ToArray<object>();
  236. object obj4 = Activator.CreateInstance(type, args);
  237. this.Inject(obj4);
  238. return obj4;
  239. }
  240. }
  241. }