public static class EnumManager<TEnum>
{
private static DataTable GetDataTable()
{
Type enumType = typeof(TEnum); // 获取类型对象
FieldInfo[] enumFields = enumType.GetFields(); //获取字段信息对象集合
DataTable table = new DataTable();
table.Columns.Add("Name", Type.GetType("System.String"));
table.Columns.Add("Value", Type.GetType("System.Int32"));
//遍历集合
foreach (FieldInfo field in enumFields)
{
if (!field.IsSpecialName)
{
DataRow row = table.NewRow();
row[0] = field.Name; // 获取字段文本值
row[1] = Convert.ToInt32(field.GetRawConstantValue()); // 获取int数值
//row[1] = (int)Enum.Parse(enumType, field.Name); 也可以这样
table.Rows.Add(row);
}
}
return table;
}
public static void SetListControl(ListControl list)
{
list.DataSource = GetDataTable();
list.DataTextField = "Name";
list.DataValueField = "Value";
list.DataBind();
}
}
public enum BookingStatus {
未提交 = 1,
已提交,
已取消,
已完成 = 6
}
EnumManager<BookingStauts>.SetListControl(ddlBookingStatus);
EnumManager<TicketStatus>.SetListControl(rblTicketStatus);