InjectRuleAttribute.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Blue
  5. {
  6. /// <summary>
  7. /// 注入规则属性---可以对接口应用属性
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Interface)]
  10. public class InjectRuleAttribute : Attribute
  11. {
  12. private List<Type> canInjectList;
  13. private Type baseInjectType;
  14. public InjectRuleAttribute(params Type[] canInject)
  15. {
  16. baseInjectType = typeof(ICanInject);
  17. canInjectList = new List<Type>(canInject.Length);
  18. foreach (var item in canInject)
  19. {
  20. if (item.GetInterfaces().Contains(baseInjectType))
  21. {
  22. canInjectList.Add(item);
  23. }
  24. }
  25. }
  26. public List<Type> GetCanInjectList()
  27. {
  28. return canInjectList;
  29. }
  30. public bool IfCanInject(Type canInject)
  31. {
  32. if (canInjectList.Contains(canInject))
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38. }
  39. }