CustomAttributeHelper.cs 4.1 KB

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