0
点赞
收藏
分享

微信扫一扫

枚举类型操作实例


//typeof 用于获取类型的 System.Type 对象。
System.Type type = typeof(int);



//通过调用 Type 对象的 GetFields 或 GetField 方法可获取 FieldInfo 对象

myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);


枚举类型的操作方法:

1.定义

public enum EmployeeType
{
    [EnumDescription("Regular Employee")]
    RegularEmploye,
    [EnumDescription("Store Manager")]
    StoreManager,
    [EnumDescription("Chain Store Manager")]
    ChainStoreManager,
    [EnumDescription("Department Manager")]
    DepartmentManager,
    [EnumDescription("On Floor Supervisor")]
    Supervisor
}

2.遍历枚举值

foreach (int iVal in Enum.GetValues(typeof(EmployeeType)))
  {
      ddlChart.Items.Add(iVal .ToString());
  }


3.遍历枚举项名称

foreach (string sName in Enum.GetNames(typeof(EmployeeType)))
  {
      ddlChart.Items.Add(sName.ToString());
  }

4.遍历枚举注释

Type type = typeof(DescriptionAttribute);
 foreach (FieldInfo fi in typeof(EmployeeType).GetFields())
   {
      object[] arr = fi.GetCustomAttributes(type, true);
      if (arr.Length > 0)
        {
            ddlChart.Items.Add(((DescriptionAttribute)arr[0]).Description);
         }
 }







举报

相关推荐

0 条评论