ServiceInjector.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. namespace Blue
  3. {
  4. /// <summary>
  5. /// Service 层注入
  6. /// </summary>
  7. public class ServiceInjector : AbstractArchitectureComponentInjector
  8. {
  9. private Type serviceType;
  10. /// <summary>
  11. /// 获取 IService 的类型
  12. /// </summary>
  13. public ServiceInjector():base(typeof(IService)) {
  14. serviceType = typeof(IService);
  15. }
  16. /// <summary>
  17. /// 根据类型获取注入的Object
  18. /// </summary>
  19. protected override object GetInjectObject(Type baseType)
  20. {
  21. object instance= architectureInstance.GetService(baseType);
  22. if (instance == null)
  23. {
  24. var interfaces = baseType.GetInterfaces();
  25. foreach (var type in interfaces)
  26. {
  27. if (serviceType.IsAssignableFrom(type)&&!serviceType.Equals(type))
  28. {
  29. instance = architectureInstance.GetService(type);
  30. if (instance != null)
  31. {
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. return instance;
  38. }
  39. }
  40. }