C# 枚举(Enum ) 应用总结
2012-09-05 16:21
477 查看
1. 枚举定义
普通情况下,枚举字段常数可以是汉字,英文,但不能是数字。当不指定值时,默认值从0开始,如下:待处理=0,审核中=1
当需要自定义值时则直接在后面赋值即可
定义枚举Description
2.枚举取值
通过枚举类型,常量名称取key
通过枚举类型,和key,获指定的常量名称
通过枚举类型,和key,获指枚举Description
普通情况下,枚举字段常数可以是汉字,英文,但不能是数字。当不指定值时,默认值从0开始,如下:待处理=0,审核中=1
public enum Status { 待处理, 审核中, 交易终止, 交易完成 }
当需要自定义值时则直接在后面赋值即可
public enum Status { 待处理 = 0, 审核中 = 10, 交易终止 = 20, 交易完成 = 99 }
定义枚举Description
public enum Status { [Description("未审核")] Untreated = 0, [Description("审核中")] Process = 10, [Description("交易终止")] Cancel = 20, [Description("交易完成")] Over= 99 }
2.枚举取值
通过枚举类型,常量名称取key
Status.待处理
通过枚举类型,和key,获指定的常量名称
Type type = typeof(Status); String enumName = Enum.GetName(type, key);
通过枚举类型,和key,获指枚举Description
public static string GetEnumDescription(Type enumType, int key) { FieldInfo EnumInfo = enumType.GetField(Enum.GetName(enumType, key)); DescriptionAttribute[] attributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } return GetEnumText(enumType, key.ToString()); }
相关文章推荐
- 一起谈.NET技术,C# 枚举在项目中的应用总结
- C# 枚举在项目中的应用总结
- enum枚举的使用总结 在android中的应用 持续更新
- C# 枚举在项目中的应用“.NET研究”总结
- 总结C#枚举在开发项目中的应用
- C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
- [原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
- C# 枚举在项目中的应用总结
- C# 枚举在项目中的应用总结 ---转自http://kb.cnblogs.com/page/83942/
- C#枚举类型enum
- C# Enum枚举转 Dictionary字典
- C# Enum枚举转 Dictionary字典
- C#程序设计与应用课程教学总结兼2014年回顾
- java enum(枚举)使用详解 + 总结
- C# 中的枚举类型 enum (属于值类型)
- [.Net码农].net 枚举(Enum)使用总结
- C# Enum,Int,String的互相转换 枚举转换
- enum 枚举应用
- Java中枚举enum的应用
- 【阶段总结】关于C# WinForm程序的一些应用总结