EnumMeta.cs 785 B

1234567891011121314151617181920212223242526
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace VCommon.Reflection
  5. {
  6. public class EnumMeta<T>
  7. {
  8. public static IReadOnlyDictionary<T, EnumMetaAttribute> Meta { get; }
  9. static EnumMeta()
  10. {
  11. if (false == typeof(T).IsEnum) throw new VCommonException("Generic type argument must be Enum.");
  12. //TODO: EnumMeta 检查枚举值重复
  13. Meta = typeof(T).GetFields().Where(p => p.FieldType == typeof(T))
  14. .ToDictionary(
  15. k => (T)k.GetRawConstantValue(),
  16. p => p.GetCustomAttributes(typeof(EnumMetaAttribute))
  17. .OfType<EnumMetaAttribute>()
  18. .Single()
  19. );
  20. }
  21. }
  22. }