NET attribute classes have a feature class name +attribute, inherit base class Attribute, we look at the feature class that Microsoft comes with:DescriptionAttribute
namespacesystem.componentmodel{//Summary://Specifies the description of the property or event. [AttributeUsage (AttributeTargets.All)] Public classDescriptionattribute:attribute {//Summary://Specifies the default value for System.ComponentModel.DescriptionAttribute, which is an empty string (""). This static field is read-only. Public Static ReadOnlyDescriptionAttribute Default; //Summary://Initializes a new instance of the System.ComponentModel.DescriptionAttribute class without parameters. PublicDescriptionAttribute (); // //Summary://Initializes a new instance of the System.ComponentModel.DescriptionAttribute class with a description. // //Parameters://Description://The description text. [Targetedpatchingoptout ("performance critical to-inline this type of method across NGen image boundaries")] PublicDescriptionAttribute (stringdescription); //Summary://Gets the description that is stored in this attribute. // //return Result://the description that is stored in this attribute. Public Virtual stringDescription {Get; } // //Summary://Gets or sets the string that is stored as a description. // //return Result://the string that is stored as the description. The default value is an empty string (""). protected stringDescriptionvalue {Get;Set; } //Summary://returns whether the value of the given object equals the current System.ComponentModel.DescriptionAttribute. // //Parameters://obj://The object to test for the equality of the value. // //return Result://true if the value of the given object equals the value of the current object; Public Override BOOLEquals (Objectobj); Public Override intGetHashCode (); // //Summary://returns a value that indicates whether this is the default System.ComponentModel.DescriptionAttribute instance. // //return Result://true if this is the default System.ComponentModel.DescriptionAttribute instance; otherwise false. Public Override BOOLIsdefaultattribute (); }}
View Code
After reading this class, we learned that this class is used to describe what the object can be class, attribute, field, interface, abstract, XXX
Let's look at a function object for the class, we define a person's type, there are two attribute names and gender
Public class person{ [Description (" name ")] publicstring getset;} [Description (" gender ")] Public int Get Set ; }}
View Code
Do you feel familiar with the code generated by many ORM tools on the Web is not like the above, but most of them are custom attribute classes
In fact, the characteristics of the class, in our actual project play a big role, I give a scene, in the process of making the report, the export Excel process, the column name and column types are very fastidious, generally we prepare the data most are the list collection
The list object is a class, we can use the attribute class to describe the Chinese name of each column, and then read the field type, basically can satisfy the export function, see a package code below
Public classReflectionhelper<t>whereT:New(){ /// <summary> ///Get attribute Information/// </summary> /// <param name= "T" ></param> /// <returns></returns> Public StaticList<propertityfieldinfo>Getpropertiyinfo () {List<PropertityFieldInfo> Listrtn =NewList<propertityfieldinfo>(); Dictionary<int,string> dicpropertiy =Newdictionary<int,string>(); Dictionary<int, type> dicfiled =Newdictionary<int, type>(); Dictionary<int,string> dicfiledname =Newdictionary<int,string>(); T obj=NewT (); varPindex =0; varProperties =obj. GetType (). GetProperties (); varFiles = obj. GetType (). GetFields (BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public); foreach(varPinchproperties) { varv = (descriptionattribute[]) p.getcustomattributes (typeof(DescriptionAttribute),false); vardesc = v[0]. Description; Dicfiledname.add (Pindex, p.name); Dicpropertiy.add (Pindex, DESC); Pindex++; } varFindex =0; foreach(varFinchfiles) { varFieldType =F.fieldtype; Dicfiled.add (Findex, FieldType); Findex++; } foreach(keyvaluepair<int,string> KVinchdicpropertiy) {Propertityfieldinfo m=NewPropertityfieldinfo (); M.name=dicfiledname[kv. Key]; M.desc=dicpropertiy[kv. Key]; M.type=dicfiled[kv. Key]; Listrtn.add (m); } returnListrtn; } /// <summary> ///get all column name Descriptions/// </summary> /// <returns></returns> Public Staticlist<string>Getpropertiydescinfo () {List<string> list =Newlist<string>(); varPropertiyinfos =Getpropertiyinfo (); foreach(varIteminchPropertiyinfos) {list. ADD (item. DESC); } returnlist; }} Public classpropertityfieldinfo{ Public stringName {Get;Set; } Public stringDesc {Get;Set; } PublicType type {Get;Set; }}
View Code
Console run down
class program { staticvoid Main (string[] args) { var dic = Reflectionhelper<person>. Getpropertiyinfo (); foreach (var in dic) { Console.WriteLine (kv. Name); Console.WriteLine (KV. Type); Console.WriteLine (KV. DESC); } Console.read (); } }
View Code
Look at the results
Reflectionhelper, this kind of method is useful to reflection and generics, about reflection and generics everyone can learn by themselves, you can extrapolate yourself to write a feature class, in fact, we blog has written
Hope this article to the small partners have help, like the point of praise, thank you!
NET Attribute Class description understand the following