using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace HStation.WinFrmUI { /// /// 字符常量辅助类 /// public class ConstStringHelper { /// /// 获取常量列表 /// /// public static SortedList GetSortedList() { var list = new SortedList(); Type type = typeof(T); var constList = BindingFlags.Static | BindingFlags.Public; var fields = type.GetFields(constList); foreach (var item in fields) { list.Add(item.Name, item.GetValue(null).ToString()); } return list; } //获取常量名列表 public static List GetKeys() { var list = new List(); Type type = typeof(T); var constList = BindingFlags.Static | BindingFlags.Public; var fields = type.GetFields(constList); if (fields == null) return default; return fields.Select(x => x.GetRawConstantValue().ToString()).ToList(); } //获取常量名列表 public static List GetValues() { var list = new List(); Type type = typeof(T); var constList = BindingFlags.Static | BindingFlags.Public; var fields = type.GetFields(constList); if (fields == null) return default; return fields.Select(x => x.Name).ToList(); } //获取常量值 public static string GetValue(string key) { Type type = typeof(T); var list = BindingFlags.Static | BindingFlags.Public; var fields = type.GetFields(list); foreach (var item in fields) { if (item.GetRawConstantValue().ToString() == key) return item.Name; } return default; } } }