直接上代码,加入有如下枚举
- public enum EMyType
- {
- [System.ComponentModel.Description("A类型")]
- TypeA = 1,
- [System.ComponentModel.Description("B类型")]
- TypeB = 1,
- [System.ComponentModel.Description("C类型")]
- TypeC = 2,
- }
- foreach (var name in Enum.GetNames(typeof(EMyType)))
- {
- Console.WriteLine(name);
- }
- foreach (var value in Enum.GetValues(typeof(EMyType)))
- {
- Console.WriteLine(string.Format("{0}={1}", value.ToString(), Convert.ToInt32(value)));
- }
- foreach (var value in Enum.GetValues(typeof(EMyType)))
- {
- object[] objAttrs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
- if (objAttrs != null &&
- objAttrs.Length > 0)
- {
- DescriptionAttribute descAttr = objAttrs[0] as DescriptionAttribute;
- Console.WriteLine(string.Format("[{0}]", descAttr.Description));
- }
- Console.WriteLine(string.Format("{0}={1}", value.ToString(), Convert.ToInt32(value)));
- }