. 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.