attribute classes in the initial C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.