本文介绍了如何获取枚举描述值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个枚举,其中有描述。我无法获得描述值。我设法得到枚举值,但我希望价值是描述值。任何解决方案? 我尝试过: public enum FaqType { [Description(Internal)] Internal = 1, [描述(网站)] 网站= 2, [描述(网络与内部)] WebInternal = 3 } 这是我处理请求和回复的实体。 [描述(类型)] 公共字符串类型 { get { return((Common.FaqType)TypeId)的ToString(); } } 解决方案 public static string ToDescription(此 System.Enum数据) { var field = data.GetType()。GetField (data.ToString()); var desAttribute = field.GetCustomAttributes( typeof (DescriptionAttribute), false ) as DescriptionAttribute []; if (desAttribute.Length > 0 ) { return desAttribute [ 0 ]。信息; } return string .Empty; } 其中说明是自定义属性,具有您在构造函数中设置的Message属性 I'm having an enum in which it has a description. i'm unable to get the description value. i managed to get the enum value but i want the value to be the description value.Any solutions?What I have tried:public enum FaqType { [Description("Internal")] Internal = 1, [Description("Website")] Website = 2, [Description("Web & Internal")] WebInternal = 3 }this is my entity in which requests and responses are handled.[Description("Type")]public string Type{ get { return ((Common.FaqType)TypeId).ToString(); }} 解决方案 public static string ToDescription(this System.Enum data) { var field = data.GetType().GetField(data.ToString()); var desAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; if (desAttribute.Length > 0) { return desAttribute[0].Message; } return string.Empty; }Where Description is Custom Attribute having Message property which you set in Constructor 这篇关于如何获取枚举描述值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 06:03