{"id":2687,"date":"2015-03-22T21:19:02","date_gmt":"2015-03-22T20:19:02","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=2687"},"modified":"2015-03-23T12:43:31","modified_gmt":"2015-03-23T11:43:31","slug":"c-get-description-attribute-from-an-enum-value","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/c-get-description-attribute-from-an-enum-value\/","title":{"rendered":"C#: Get description attribute from an enum value"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_2687 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_2687')){$('.twoclick_social_bookmarks_post_2687').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"C%23%3A%20Get%20description%20attribute%20from%20an%20enum%20value\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Ain%20this%20post%20I%20will%20show%20how%20to%20read%20a%20description%20attribute%20of%20an%20enum%20value%20by%20reflection.%20Let%20us%20assume%20we%20have%20a%20enum%20linke%20this%0D%0A%5Bcsharp%5D%0D%0Apublic%20enum%20MetalBands%20%7B%0D%0A%09%5BDescriptionAttribute%28%26quot%3BThis%20is%20Metallica%20from%20CA%26quot%3B%29%5D%0D%0A%09Metallica%2C%0D%0A%09%5BDescriptionAttribute%28%26quot%3BThis%20is%20Slayer%20from%20CA%26quot%3B%29%5D%0D%0A%09Slayer%2C%0D%0A%09%5BDescriptionAttribute%28%26quot%3BThis%20is%20Overkill%20from%20NY%26quot%3B%29%5D%0D%0A%09Overkil%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/c-get-description-attribute-from-an-enum-value\\\/\",\"post_id\":2687,\"post_title_referrer_track\":\"C%23%3A+Get+description+attribute+from+an+enum+value\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>in this post I will show how to read a description attribute of an enum value by reflection. Let us assume we have a enum linke this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic enum MetalBands {\r\n\t&#x5B;DescriptionAttribute(&quot;This is Metallica from CA&quot;)]\r\n\tMetallica,\r\n\t&#x5B;DescriptionAttribute(&quot;This is Slayer from CA&quot;)]\r\n\tSlayer,\r\n\t&#x5B;DescriptionAttribute(&quot;This is Overkill from NY&quot;)]\r\n\tOverkill\r\n};\r\n<\/pre>\n<p><!--more--><br \/>\nTwo steps are necessary<br \/>\nGet the Fieldinfo of the the enum value<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSystem.Reflection.FieldInfo oFieldInfo=MetalBands.Metallica.GetType().GetField(MetalBands.Metallica.ToString());\r\n<\/pre>\n<p>and get all attributes of type <strong>DescriptionAttribute<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDescriptionAttribute&#x5B;] attributes = (DescriptionAttribute&#x5B;])oFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);\r\n<\/pre>\n<p>if an attribute exists print it. We have only one definied , therefore the attribute is at index 0<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (attributes.Length &gt; 0)\r\n{\r\n     System.Console.WriteLine(attributes&#x5B;0].Description);\r\n}\r\n<\/pre>\n<p>That&#8217;s it.<br \/>\nAnd as a function \ud83d\ude42<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static string GetDescription(MetalBands Band)\r\n{\r\n     System.Reflection.FieldInfo oFieldInfo = Band.GetType().GetField(Band.ToString());\r\n     DescriptionAttribute&#x5B;] attributes = (DescriptionAttribute&#x5B;])oFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);\r\n     if (attributes.Length &gt; 0)\r\n     {\r\n          return attributes&#x5B;0].Description;\r\n     }\r\n     else\r\n     {\r\n          return Band.ToString();\r\n     }\r\n}\r\nSystem.Console.WriteLine(GetDescription(MetalBands.Metallica));\r\n<\/pre>\n<p>Have fun<br \/>\nMichael<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, in this post I will show how to read a description attribute of an enum value by reflection. Let us assume we have a enum linke this public enum MetalBands { &#x5B;DescriptionAttribute(&quot;This is Metallica from CA&quot;)] Metallica, &#x5B;DescriptionAttribute(&quot;This is Slayer from CA&quot;)] Slayer, &#x5B;DescriptionAttribute(&quot;This is Overkill from NY&quot;)] Overkill };<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[150],"tags":[860,642,643,73,247],"class_list":["post-2687","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-description-attribute","tag-enum","tag-get","tag-read"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=2687"}],"version-history":[{"count":8,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2687\/revisions"}],"predecessor-version":[{"id":2695,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/2687\/revisions\/2695"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=2687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=2687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=2687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}