AbstractEnum.cs 792 B

123456789101112131415161718192021
  1. using System;
  2. using System.Collections.Generic;
  3. namespace JiaZhiQuan.Common.ClassEnum {
  4. public abstract class AbstractEnum<TEnum> where TEnum : notnull, AbstractEnum<TEnum> {
  5. protected static readonly Type enumType = typeof(TEnum);
  6. protected readonly static List<TEnum> Values = ListEnumProperties();
  7. private static List<TEnum> ListEnumProperties() {
  8. List<TEnum> values = new List<TEnum>();
  9. var members = enumType.GetProperties();
  10. foreach (var item in members) {
  11. if (item.PropertyType == enumType) {
  12. TEnum? value = (TEnum?)item.GetValue(enumType);
  13. if (value != null) { values.Add(value); }
  14. }
  15. }
  16. return values;
  17. }
  18. }
  19. }