. Net basics-Attribute

Source: Internet
Author: User

. Net basics-Attribute

In my new project, what I saw most at the Entity layer is what I learned by the winning bidder, mainly because it completed the ORM. It is not clear how to implement it, but it certainly has a lot to do with it. So let's take a look at it first, so that we can unlock the true veil of the implementation process!

First, the custom usage of the Attribute class is marked in the figure. Generally, Attribute is used in this way:

First, define an Attribute class;

Here we define a custom attribute class.

[AttributeUsage(AttributeTargets.All)]    public class DeveloperAttribute : Attribute    {        private string name;        private string level;        private bool reviewed;        public DeveloperAttribute(string name, string level)        {            this.name = name;            this.level = level;            this.reviewed = false;        }        public virtual string Name { get { return name; } }        public virtual string Level        { get { return level; } }        public virtual bool Reviewed        {            get { return reviewed; }            set { reviewed = value; }        }    }

Second, add the Attribute class to the corresponding class.

Here I create a UserInfo class and attach an attribute object to it, as shown below:

[Developer ("zhangsan", "5", Reviewed = true)] public class UserInfo {// code is omitted for highlighting... }
Third, obtain the phase value of the attribute object on the attachment class.

Here, the value of attribute is put in the UserInfo class. Let's see how I get and use attribute object attributes.

[Developer ("zhangsan", "5", Reviewed = true)] public class UserInfo {public UserInfo () {System. reflection. memberInfo info = typeof (UserInfo); Attributes Attribute att = (attributes Attribute) Attribute. getCustomAttribute (info, typeof (partition attribute); // This method is mainly used to obtain the if (att! = Null) {this. userName = att. name; this. level = att. level; this. isChild = att. reviewed ;}} private string _ name; public string UserName {set {_ name = value;} get {return _ name ;}} public string Level {set; get ;} public bool IsChild {set; get ;}}


Summary:

The instance above allows the attribute class and the userinfo class to complete the attribute value transfer, probably the attribute object attached to the entity in the above ORM will also be passed through this value, achieving the association between entities and database tables is the next direction of thinking.




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.