C# Enum枚举操作
C# Enum枚举转DataTable
public static DataTable EnumToDataTable(Type enumType, string NameFileName, string valFileName) { string[] Names = System.Enum.GetNames(enumType); Array Values = System.Enum.GetValues(enumType); DataTable table = new DataTable(); table.Columns.Add(NameFileName, System.Type.GetType("System.String")); table.Columns.Add(valFileName, System.Type.GetType("System.Int32")); table.Columns[NameFileName].Unique = true; for (int i = 0; i < Values.Length; i++) { DataRow DR = table.NewRow(); DR[NameFileName] = Names[i].ToString(); DR[valFileName] = (int)Values.GetValue(i); table.Rows.Add(DR); } return table; }
GarsonZhang www.infnitee.com
C#根据枚举名称获得枚举值
/// <summary> /// 根据枚举名称获得枚举值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public T? ConvertFromName<T>(string name) where T : struct // Enum { T value; if (Enum.TryParse<T>(name, out value) == false) { return null; } return value; }
GarsonZhang www.yesdotnet.com
C# 枚举转字典对象列表
/// <summary> /// 根据枚举名称获得枚举值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public List<EnumItemObject> ConvertToDict<T>() where T : struct // Enum { string[] Names = System.Enum.GetNames(typeof(T)); Array Values = System.Enum.GetValues(typeof(T)); List<EnumItemObject> data = new List<EnumItemObject>(); for (int i = 0; i < Values.Length; i++) { data.Add(new EnumItemObject() { Name = Names[i], Value = (int)Values.GetValue(i) }); } return data; } public class EnumItemObject { public string Name { get; set; } public int Value { get; set; } }
GarsonZhang www.yesdotnet.com
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
CommonCode YES开发框架