枚举常⽤⽅法枚举转List集合根据枚举的value获得对应的
name
/// <summary>
/// 枚举的名称和数值实体类
/// </summary>
public class NameValue
{
//枚举的名称
public string Name { get; set; }
//枚举的数值
public int Value { get; set; }
}
/// <summary>
/// 枚举转List集合
/// </summary>
/// <typeparam name="T">枚举</typeparam>
/// <returns></returns>
public static List<NameValue> EnumToList<T>()
{
List<NameValue> list = new List<NameValue>();
foreach (var e in Enum.GetValues(typeof(T)))//枚举转List
{
object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
NameValue nv = new NameValue();
typeof的用法nv.Value = Convert.ToInt32(e);
nv.Name = e.ToString();
list.Add(nv);
}
return list;
}
/// <summary>
/// 枚举转KeyValuePair集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<KeyValuePair<int, string>> EnumToKeyValuePair<T>()
{
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
foreach (var e in Enum.GetValues(typeof(T)))//枚举转List
{
object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
int Key = Convert.ToInt32(e);
string Value = e.ToString();
KeyValuePair<int, string> kv = new KeyValuePair<int, string>(Key, Value);
list.Add(kv);
}
return list;
}
/// <summary>
/// 枚举转Dictionary集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<Dictionary<int, string>> EnumToDictionary<T>()
{
List<Dictionary<int, string>> list = new List<Dictionary<int, string>>();
foreach (var e in Enum.GetValues(typeof(T)))//枚举转List
{
object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
Dictionary<int, string> kv = new Dictionary<int, string>();
int Key = Convert.ToInt32(e);
string Value = e.ToString();
string Value = e.ToString();
kv.Add(Key, Value);
list.Add(kv);
}
return list;
}
/// <summary>
/// 根据枚举的value获得对应的name
/// </summary>
/// <typeparam name="T">枚举</typeparam>
/// <param name="value">数值</param>
/// <returns>名称</returns>
public static string GetNameByValue<T>(int value) {
string str = "";
try
{
str = Enum.GetName(typeof(T), value);
}
catch { }
return str;
}
/// <summary>
/// 根据枚举的name获得对应的value值
/// </summary>
/// <typeparam name="T">枚举</typeparam>
/// <param name="name">名称</param>
/
// <returns>数值</returns>
public static int GetValueByName<T>(string name) {
if (string.IsNullOrWhiteSpace(name)) return -1; var str = -1;
try
{
str = (int)Enum.Parse(typeof(T), name);
}
catch { }
return str;
}
/// <summary>
/
// 获取枚举name的数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static string[] GetNames<T>()
{
return Enum.GetNames(typeof(T));
}
/// <summary>
/// 获取枚举value的数组
/// </summary>
/
// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Array GetValues<T>()
{
return Enum.GetValues(typeof(T));
}
⽤法举例:
public class NameValue { public string Name { get; set; } public int Value { get; set; } }
public enum ABC{a,b,c}
var list = EnumHelper.EnumToList<ABC>();//枚举转List集合
var list = EnumHelper.EnumToKeyValuePair<ABC>();//枚举转KeyValuePair集合
var list = EnumHelper.EnumToDictionary<ABC>();//枚举转Dictionary集合
var name = EnumHelper.GetNameByValue<ABC>(1);//输出:"b" 根据枚举的value获得对应的name var key = EnumHelper.GetValueByName<ABC>("c");//输出:2 根据枚举的name获得对应的value
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论