Reflections and properties
The. NET Framework provides reflection and feature technology that can be used to check data repeatability to determine whether a piece of data is written to a database table.
Demand
When an entity writes a data to a database, many times, it is necessary to check that the data is not a duplicate data, such as the new person, assuming that the ID number can not be duplicated, the new person ID number and the person table of one or more duplicate, at this time need to give hints or take other means, such as update, or delete.
Method
In this scenario, you can use the features and reflection technologies provided by the. NET Framework to address such requirements. Specific process,
Second, in the entity class, reference the newly-written attribute class to construct its unique identity (one or more attribute combinations);
Finally, when checking data repeatability, use the method provided by attribute to obtain a unique identity attribute (one or more) for each entity class.
Attribute[] GetCustomAttributes(modeltype, inherit);
Keyfieldattribute Attribute Class
public class Keyfieldattribute:attribute { private static list<string> keyfields = new list<string > (); <summary>///Construction Key Properties///</summary>// <param name= "Fields" ></param> Public Keyfieldattribute (params string[] fields) { foreach (String kf, fields) { if (!keyfields. Contains (KF)) Keyfields. ADD (KF); } } public static list<string> keyfields { get {return keyfields;} }
Entity Class model
[Keyfield ("id")]public class person{public int ID {get;set;}//Personnel ID public string Name {get;set;} Person name Public DateTime BirthDate {get;set;}//Date of birth} [Keyfield ("Rolegroupid", "Rolecode")] public class Role {public int rolegroupid {get; set}//Group ID public string rolecode {get; set;}/Role number public St Ring RoleName {get; set;} Role name }
Note that the attribute extension class, here is the suffix attribute in Keyfieldattribute can be omitted, so Keyfield is shorthand, equivalent to Keyfieldattribute.
Use the attribute class:
Keyfieldattribute.getcustomattributes (typeof (Person), true); list<string> fields = Keyfieldattribute.keyfields; Gets the unique identity attribute ID of the person entity Keyfieldattribute.getcustomattributes (typeof (Role), true); var fields = keyfieldattribute.keyfields;//role Entity unique attribute, 2 attribute combinations: Rolegroupid,rolecode
Using the key attribute combination returned by the attribute, query the data in the database, if can find at least one record, then follow certain logic processing; if not, you can write the new data directly. Description of the methods provided in the attribute class:
<summary>///retrieves an array of custom attributes that are applied to the members of the type. </summary>///<param name= "Modeltype" > type of custom attribute to search </param>///<param name= "Inherit" > Whether to search for the ancestors of the member </param>///<returns> the array of custom attributes </returns>attribute[] GetCustomAttributes (Modeltype, Inherit);
The above is the C # reflection and characteristics of the detailed description of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!