C#对枚举的常用操作
2012-05-06 15:08
435 查看
枚举操作类:
枚举定义:
客户端调用:
public partial class XEnumHelper { /// <summary> /// 获得枚举的所有枚举项 /// </summary> /// <param name="enumType"></param> /// <returns></returns> public static IList<string> GetEnums(Type enumType) { IList<string> enums = new List<string>(); foreach (string s in Enum.GetNames(enumType)) { enums.Add(s); } return enums; } /// <summary> /// 将字符串转换为枚举 /// </summary> /// <param name="enumType">枚举类型</param> /// <param name="strValue">字符串值</param> /// <returns></returns> public static object String2Enum(Type enumType, string strValue) { return Enum.Parse(enumType, strValue); } }
枚举定义:
public enum OperateType { None = 0, Add = 1, Edit = 2, Delete = 3 }
客户端调用:
static void Main(string[] args) { //获取枚举的常数值 OperateType opreateType = OperateType.Edit; int num = (int)opreateType; Console.WriteLine("枚举项Edit的常数值为:{0}", num); //字符串转换为枚举值 string optType = "Delete"; opreateType = (OperateType)XEnumHelper.String2Enum(typeof(OperateType), optType); Console.WriteLine("字符串值:{0}转换为枚举类型:{1}", optType, opreateType); //通过枚举的常数值获得枚举值 num = 3; opreateType = (OperateType)num; Console.WriteLine("枚举项常数值为3对应的枚举项为:{0}", opreateType); //列举枚举值 IList<string> enums = XEnumHelper.GetEnums(typeof(OperateType)); int loopNo = 0; foreach (string e in enums) { loopNo += 1; Console.WriteLine("枚举项{0}:{1}", loopNo, e); } Console.Read(); }
相关文章推荐
- C#基础(六)--枚举的一些常用操作
- C#Enum常用枚举操作
- C#枚举变量常用操作
- (c#)Excel常用格式操作 .
- 枚举的一些常用操作
- C#_DataGridView控件常用操作
- C#操作Excell常用方法
- 黑马程序员-C#常用字符串操作
- [C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
- C# 枚举器 手动实现枚举器 可枚举集合 枚举器操作 迭代器
- C#中一些字符串操作的常用用法
- C#中对DatagridView的部分常用操作
- C#中一些字符串操作的常用用法
- C#中常用的文件操作方法
- C#字符串的常用操作工具类代码分享
- C#中对DatagridView的部分常用操作
- C#操作WORD常用东东
- C#中一些字符串操作的常用用法
- C# 常用文件操作 文本文件 二进制文件 读写操作
- C#中一些字符串操作的常用用法,c#编码和解码