AttributeTool.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Y.Utils.ReflectionUtils.AttributeUtils
  11. {
  12. public static class AttributeTool
  13. {
  14. /// <summary>
  15. /// Cache Data
  16. /// </summary>
  17. private static readonly Dictionary<string, string> Cache = new Dictionary<string, string>();
  18. /// <summary>
  19. /// 获取CustomAttribute Value
  20. /// </summary>
  21. /// <typeparam name="T">Attribute的子类型</typeparam>
  22. /// <param name="sourceType">头部标有CustomAttribute类的类型</param>
  23. /// <param name="attributeValueAction">取Attribute具体哪个属性值的匿名函数</param>
  24. /// <returns>返回Attribute的值,没有则返回null</returns>
  25. public static string GetCustomAttributeValue<T>(this Type sourceType, Func<T, string> attributeValueAction) where T : Attribute
  26. {
  27. return GetAttributeValue(sourceType, attributeValueAction, null);
  28. }
  29. /// <summary>
  30. /// 获取CustomAttribute Value
  31. /// </summary>
  32. /// <typeparam name="T">Attribute的子类型</typeparam>
  33. /// <param name="sourceType">头部标有CustomAttribute类的类型</param>
  34. /// <param name="attributeValueAction">取Attribute具体哪个属性值的匿名函数</param>
  35. /// <param name="name">field name或property name</param>
  36. /// <returns>返回Attribute的值,没有则返回null</returns>
  37. public static string GetCustomAttributeValue<T>(this Type sourceType, Func<T, string> attributeValueAction,
  38. string name) where T : Attribute
  39. {
  40. return GetAttributeValue(sourceType, attributeValueAction, name);
  41. }
  42. private static string GetAttributeValue<T>(Type sourceType, Func<T, string> attributeValueAction,
  43. string name) where T : Attribute
  44. {
  45. var key = BuildKey(sourceType, name);
  46. if (!Cache.ContainsKey(key))
  47. {
  48. CacheAttributeValue(sourceType, attributeValueAction, name);
  49. }
  50. return Cache[key];
  51. }
  52. /// <summary>
  53. /// 缓存Attribute Value
  54. /// </summary>
  55. private static void CacheAttributeValue<T>(Type type,
  56. Func<T, string> attributeValueAction, string name)
  57. {
  58. var key = BuildKey(type, name);
  59. var value = GetValue(type, attributeValueAction, name);
  60. lock (key + "_attributeValueLockKey")
  61. {
  62. if (!Cache.ContainsKey(key))
  63. {
  64. Cache[key] = value;
  65. }
  66. }
  67. }
  68. private static string GetValue<T>(Type type,
  69. Func<T, string> attributeValueAction, string name)
  70. {
  71. object attribute = null;
  72. if (string.IsNullOrEmpty(name))
  73. {
  74. attribute =
  75. type.GetCustomAttributes(typeof(T), false).FirstOrDefault();
  76. }
  77. else
  78. {
  79. var propertyInfo = type.GetProperty(name);
  80. if (propertyInfo != null)
  81. {
  82. attribute =
  83. propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
  84. }
  85. var fieldInfo = type.GetField(name);
  86. if (fieldInfo != null)
  87. {
  88. attribute = fieldInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
  89. }
  90. }
  91. return attribute == null ? null : attributeValueAction((T)attribute);
  92. }
  93. /// <summary>
  94. /// 缓存Collection Name Key
  95. /// </summary>
  96. private static string BuildKey(Type type, string name)
  97. {
  98. if (string.IsNullOrEmpty(name))
  99. {
  100. return type.FullName;
  101. }
  102. return type.FullName + "." + name;
  103. }
  104. }
  105. }