Foreword: In C # development, sometimes we need to read the information in Attribute (about Attribute , I myself think of him as a class, attribute tag, this tag can give you some additional information about classes, methods, properties)
How we get information about these tokens and how to get custom attribute information.
Body:
1. Get more information about an enumeration
Suppose we have such an enumeration
public enum Category {//<summary>///English// </summary> 中文版, //< summary>///Chinese// </summary> Chinese,//<summary>//Japanese// </ summary> Japanese }
Now I want to get the descriptive information for each enumeration. If you can't attribute, maybe you can only get it this way.
Static class program { static void Main () { var category = Category.chinese; Switch (category) {case Category.Chinese:Console.WriteLine ("Chinese"); Case Category.English:Console.WriteLine ("English"); break; Case Category.Japanese:Console.WriteLine ("Japanese"); Break;}}}
It seems that several case statements can also accomplish the task. But if the number of enumerated items is very large, then this kind of writing is undoubtedly ugly.
----------------------------------Ornate Split Line------------------------------------
Next, explore a simple application scenario for attribute
modifying enumerations
Public enum Category { //<summary>//English//</summary> [Description ("Western Language")] English, ///<summary>//Chinese//</summary> [Description ("Chinese")] Chinese, //<summary>//Japanese//</summary> [Description ("Japanese language")] Japanese }
Each enumeration entry joins the DescriptionAttribute of the System.ComponentModel namespace, and it's not clear that you can MSDN.
Then attach two to my original class ... Remember the layer in the company used to directly get Descripton tag method, but no source code, I spent a day to inquire about this problem, finally wrote their own Help class. Dedicated to everyone.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Reflection; namespace zytools{//<summary>///Enumeration helper class//</summary> public class Enumhelper {// <summary>////Get attribute of enumerated entries///</summary>//<typeparam name= "T" > Custom attribute& lt;/typeparam>//<param name= "source" > Enumeration </param>//<returns> returns enumeration, otherwise returns Null</retur ns> public static T getcustomattribute<t> (Enum source) where T:attribute {Type source Type = source. GetType (); String sourceName = Enum.getname (sourcetype, source); FieldInfo field = Sourcetype.getfield (SourceName); object[] attributes = field. GetCustomAttributes (typeof (T), false); foreach (object attribute in attributes) {if (attribute is T) return (t) attr Ibute; } return NULL; }///<summary>//Get DescriptionAttribute Description///</summary>/<param name= "so Urce "> Enumeration </param>//<returns> description tag, return tag description, otherwise return null</returns> public static St Ring GetDescription (Enum source) {var attr = Getcustomattribute<system.componentmodel.descriptionatt Ribute> (source); if (attr = = null) return null; Return attr. Description; } }}
Now all we have to do is write the code.
var category = Category.chinese; Console.WriteLine (enumhelper.getdescription (category));
is not very refreshing. hope this article is helpful to beginners, from www.xiaoniusoft.com, please specify. Love programming love life!!!
. NET C # get custom attribute