using System.ComponentModel; using System.Reflection; namespace DS.Module.Core; /// /// 常量帮助类 /// public static class ConstUtil { public static void GetConstantField() { Type t = typeof(MultiLanguageConst); FieldInfo[] fis = t.GetFields(); // 注意,这里不能有任何选项,否则将无法获取到const常量 Dictionary dicFruitCode = new Dictionary(); foreach (var field in fis) { var codeValue = field.GetRawConstantValue(); var des = string.Empty; object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性 if (objs == null || objs.Length == 0) //当描述属性没有时,直接返回名称 { dicFruitCode.Add(codeValue.ToString(), field.Name); } else { // return value; DescriptionAttribute attribute = (DescriptionAttribute)objs[0]; dicFruitCode.Add(codeValue.ToString(), attribute?.Description); } } // foreach(var item in dicFruitCode) // { // Console.WriteLine("FieldName:{0}={1}",item.Value,item.Key); // } } public static Dictionary GetMultiLanguageFields() { Type t = typeof(MultiLanguageConst); FieldInfo[] fis = t.GetFields(); // 注意,这里不能有任何选项,否则将无法获取到const常量 Dictionary dicCode = new Dictionary(); foreach (var field in fis) { var codeValue = field.GetRawConstantValue(); var des = string.Empty; object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性 if (objs == null || objs.Length == 0) //当描述属性没有时,直接返回名称 { dicCode.Add(codeValue.ToString(), field.Name); } else { // return value; DescriptionAttribute attribute = (DescriptionAttribute)objs[0]; dicCode.Add(codeValue.ToString(), attribute?.Description); } } return dicCode; } }