I've encountered this class in my recent project, and I've looked at MSDN, so to speak.
Attribute class: represents the base class for a custom property.
See is a bit of a circle, what is this for?
Attribute class
After looking up some information from the Internet, it is a bit clear that it is like an attachment to define additional information. Let me give you an example! When we do the process development, how to determine who developed the method? Of course, you can add a comment to the method to show who knocked the method, and what else? Is there any other way?
Then this class comes in handy. By inheriting this class, you can customize an attribute class, and then attach the form to add additional properties for the class or method that you want to add.
Demo Show
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >///<summary>// Custom Additional Properties class// </summary> class Classattribute:attribute { private string Name {set; get;} Public ClassAttribute (String Name) { this.name = Name; } } </span>
The above is the class that customizes the extra properties, so how to use it?
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > [Class ("Student Class", age=12)] Class Student/// default the first unassigned assignment for the constructor type, the constructor { [class] ("Football"). )] public string profession; public string profession { get {return profession;} set {profession = value;} } } </span>
It's just so simple. You can add extra attributes for classes, interfaces, methods, and so on, by playing the label.
So at this point, you might ask, I've added, so how do I read the extra attributes I added?
The attribute class has more than 20 getcustomattribute methods that allow you to read the additional properties of the definition.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" > static void Main (string[] args) { //Use reflection to read attribute System.Reflection.MemberInfo info = typeof ( Student); The information of the student class is obtained by reflection classattribute hobbyattr = (classattribute) Attribute.GetCustomAttribute (info, typeof ( ClassAttribute)); if (hobbyattr! = null) { Console.WriteLine ("Student Age: {0}", hobbyattr.age);} } </span>
This is done by calling the GetCustomAttribute method to read the extra property actions taken from the definition. Let's summarize it with a picture below.
Summary
You can imagine, from the macro-aware of the role of this kind, the future of those places can be used, more convenient and concise.
attribute classes in the initial C #