asp.Net列挙ファイルの数字はDropDownListにバインドされます

6228 ワード

 
  
public class AppEnum
{
public enum PointLogType : int
{
///
/// Email
///

[Description("Email ")]
Recruit = 1, // +
///
///
///

[Description(" ")]
Veteran = 2, //
///
///
///

[Description(" ")]
CreateOrder = 3, // -
///
///
///

[Description(" ")]
AbandonSO = 5, //
///
///
///

[Description(" ")]
CancelAbandonSO = 6, // -
///
///
///

[Description(" ")]
ReturnProduct = 7, // -
///
///
///

[Description(" ")]
CancelReturn = 8,
///
///
///

[Description(" ")]
CancelOutstock = 9, //
///
///
///

[Description(" ")]
TransferPoint = 10, //
///
///
///

[Description(" ")]
AddPointLater = 11, //
///
///
///

[Description(" ")]
UpdateSO = 12, // SaleOrder
///
///
///

[Description(" ")]
WholeSale = 13, // -, 。
///
///
///

[Description(" ")]
InfoProduct = 14, // -
///
///
///

[Description(" ")]
BizRequest = 15, //Request
///
///
///

[Description(" ")]
Remark = 16, //Remark
///
///
///

[Description(" ")]
NewRegister = 17, //
///
/// DIY
///

[Description("DIY ")]
DIY = 18, //DIY , DIY , 。
///
///
///

[Description(" ")]
SysTransferPoint = 19, // neweggcs
///
///
///

[Description(" ")]
AddPointToSysAccounts = 20, //
///
///
///

[Description(" ")]
BetReductPoint = 21, //
///
///
///

[Description(" ")]
BetAddPoint = 22, //
///
///
///

[Description(" ")]
NewCustomerFirstBuy = 23, // ,
///
///
///

[Description(" ")]
SetScoreAuto = 24, //
///
///
///

[Description(" ")]
MKTCampaign = 25,
///
///
///

[Description(" ")]
DisusePoint = -1
}
}

上は列挙リストですが、どうやって読みますか?DDRでバインドしますか?
 
  
ddlType.DisplayMember = "Value";
ddlType.ValueMember = "Key";
ddlType.DataSource = CommonFunctions.GetEnumItems(typeof(AppEnum.PointLogType), false);
ddlType.SelectedValue = 25; //

次はCommonFunctionsのGetEnumItemsメソッドです.
 
  
///
/// 。
///

///
/// "All"
///
public static List GetEnumItems(Type enumType, bool withAll)
{
List list = new List();

if (enumType.IsEnum != true)
{
//
throw new InvalidOperationException();
}

// All
if (withAll == true)
list.Add(new EnumItem(AppConst.IntNull, "All"));

// Description
Type typeDescription = typeof(DescriptionAttribute);

// ( static )
System.Reflection.FieldInfo[] fields = enumType.GetFields();

//
foreach (FieldInfo field in fields)
{
// ,
if (field.FieldType.IsEnum == false)
continue;

//
int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
string text = string.Empty;

// , Description
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
// Description ,
DescriptionAttribute aa = (DescriptionAttribute)arr[0];

//
text = aa.Description;
}
else
{
// ,
text = field.Name;
}
list.Add(new EnumItem(value, text));
}

return list;
}
public class EnumItem
{
private object m_key;
private object m_value;

public object Key
{
get { return m_key; }
set { m_key = value; }
}

public object Value
{
get { return m_value; }
set { m_value = value; }
}

public EnumItem(object _key, object _value)
{
m_key = _key;
m_value = _value;
}
}