using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Reflection;
|
|
namespace HStation.WinFrmUI
|
{
|
/// <summary>
|
/// 字符常量辅助类
|
/// </summary>
|
public class ConstStringHelper
|
{
|
/// <summary>
|
/// 获取常量列表
|
/// </summary>
|
/// <returns></returns>
|
public static SortedList<string, string> GetSortedList<T>()
|
{
|
var list = new SortedList<string, string>();
|
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<string> GetKeys<T>()
|
{
|
var list = new List<string>();
|
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<string> GetValues<T>()
|
{
|
var list = new List<string>();
|
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<T>(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;
|
}
|
}
|
}
|