动态获取enum的值并且添到List中

  public static IList<T> GetValues<T>()   

  {

      Type enumType = typeof(T);
      if (!enumType.IsEnum)

      {

        return null;

      }
      IList<T> list = new List<T>();
      var fields = from field in enumType.GetFields()

             where field.IsLiteral

             select field;
      foreach (FieldInfo field in fields)

      {

        object value = field.GetValue(enumType);

        list.Add((T)value);

      }
      returnlist;

    }

调用:

IList<City> list = Common.GetValues<City>();

原文地址:https://www.cnblogs.com/lisengl/p/2546578.html