You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.ComponentModel;
using System.Reflection;
namespace DS.Module.Core;
/// <summary>
/// 常量帮助类
/// </summary>
public static class ConstUtil
{
public static void GetConstantField()
{
Type t = typeof(MultiLanguageConst);
FieldInfo[] fis = t.GetFields(); // 注意这里不能有任何选项否则将无法获取到const常量
Dictionary<string, string> dicFruitCode = new Dictionary<string, string>();
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<string, string> GetMultiLanguageFields()
{
Type t = typeof(MultiLanguageConst);
FieldInfo[] fis = t.GetFields(); // 注意这里不能有任何选项否则将无法获取到const常量
Dictionary<string, string> dicCode = new Dictionary<string, string>();
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;
}
}