CustomAttributeHelper.cs 4.0 KB

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