
{"id":415,"date":"2010-04-02T01:26:07","date_gmt":"2010-04-02T05:26:07","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=415"},"modified":"2010-04-02T01:26:07","modified_gmt":"2010-04-02T05:26:07","slug":"net-it-is-possible-to-apply-attributes-to-enum-members","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=415","title":{"rendered":".NET: It is possible to apply attributes to enum members"},"content":{"rendered":"<p>For a long time I was sure it is not supported, because there was no corresponding <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/b3787ac0%28VS.80%29.aspx\">AttributeTargets<\/a> value.<\/p>\n<p>It turns out, that you can do it by using <code>AttributeTargets.Field<\/code>. Declaring your attribute class:<\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\">[<span style=\"color: #2b91af;\">AttributeUsage<\/span>(<span style=\"color: #2b91af;\">AttributeTargets<\/span>.Field)]<\/p>\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">public<\/span> <span style=\"color: blue;\">class<\/span> <span style=\"color: #2b91af;\">CategoryAttribute<\/span> : <span style=\"color: #2b91af;\">Attribute<\/span><\/p>\n<\/div>\n<p>Declaring your decorated enum:<\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">enum<\/span> <span style=\"color: #2b91af;\">MyEnum<\/span><\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; [<span style=\"color: #2b91af;\">Category<\/span>(<span style=\"color: #a31515;\">&quot;foo&quot;<\/span>)]<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; Foo,<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; [<span style=\"color: #2b91af;\">Category<\/span>(<span style=\"color: #a31515;\">&quot;This is bar&quot;<\/span>)]<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; Bar,<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; Baz<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<\/div>\n<p>Reading attribute values:<\/p>\n<div style=\"font-family: Courier New; font-size: 10pt; color: black; background: white;\">\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">foreach<\/span> (<span style=\"color: #2b91af;\">MyEnum<\/span> x <span style=\"color: blue;\">in<\/span> <span style=\"color: #2b91af;\">Enum<\/span>.GetValues(<span style=\"color: blue;\">typeof<\/span>(<span style=\"color: #2b91af;\">MyEnum<\/span>)))<\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">MemberInfo<\/span>[] info = <span style=\"color: blue;\">typeof<\/span>(<span style=\"color: #2b91af;\">MyEnum<\/span>).GetMember(x.ToString());<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">if<\/span> (info.Length != 1)<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Console<\/span>.WriteLine(<span style=\"color: #a31515;\">&quot;Get {0} members for {1}&quot;<\/span>, info.Length, x);<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">continue<\/span>;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Attribute<\/span>[] categories = <span style=\"color: #2b91af;\">Attribute<\/span>.GetCustomAttributes(info[0], <span style=\"color: blue;\">typeof<\/span>(<span style=\"color: #2b91af;\">CategoryAttribute<\/span>));<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">if<\/span> (categories.Length == 0)<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Console<\/span>.WriteLine(<span style=\"color: #a31515;\">&quot;{0}: No category&quot;<\/span>, x);<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color: blue;\">else<\/span><\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">CategoryAttribute<\/span> category = (<span style=\"color: #2b91af;\">CategoryAttribute<\/span>)categories[0];<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: #2b91af;\">Console<\/span>.WriteLine(<span style=\"color: #a31515;\">&quot;{0}: {1}&quot;<\/span>, x, category.Category);<\/p>\n<p style=\"margin: 0px;\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>For a long time I was sure it is not supported, because there was no corresponding AttributeTargets value. It turns out, that you can do it by using AttributeTargets.Field. Declaring <a href=\"https:\/\/ikriv.com\/blog\/?p=415\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8],"tags":[],"class_list":["entry","author-ikriv","post-415","post","type-post","status-publish","format-standard","category-cs"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}