by
0 13
0
4
3,241
0
Top 1% !
Popular
Famous
Specified
Popularity: 3952nd place
This snippet is Public
No tags for this snippet yet.
InputName0value
InputType0Enum
LanguageCSharp
LicenseMIT_X11
MethodNameGetDescription
OutputTypestring
SourceGithub
SourceMyGists

enum value map to string

enum value map to string: 
EnumExtensions.cs
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/21128/enum-value-map-to-string?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/// <summary> /// http://stackoverflow.com/questions/2787506/cast-string-to-enum-with-enum-attribute /// todo: http://www.cnblogs.com/smalldust/archive/2006/04/25/384657.html 利用缓存优化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static string GetDescription(this Enum value) { if (value == null) { throw new ArgumentNullException("value"); } string description = value.ToString(); FieldInfo fieldInfo = value.GetType().GetField(description); DescriptionAttribute[] attributes = (DescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { description = attributes[0].Description; } return description; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus