Use reflection and features to simplify code and reflection features to simplify code
Suppose there is a Student)
/// <Summary> /// Student class /// </summary> public class Student {// <summary> /// name /// </summary> private string name; public string Name {get {return name;} set {name = value ;}} /// <summary> /// Age /// </summary> public int Age {get; set ;} /// <summary> /// Address /// </summary> public string Address {get; set ;} /// <summary> /// gender /// </summary> public string Sex ;}
If you want to determine whether some fields (attributes) are null or greater than 0, you can run the following code:
Public static string ValidateStudent (Student student) {StringBuilder validateMessage = new StringBuilder (); if (string. isNullOrEmpty (student. name) {validateMessage. append ("name cannot be blank");} if (string. isNullOrEmpty (student. sex) {validateMessage. append ("gender cannot be blank");} if (student. age <= 0) {validateMessage. append ("age must be greater than 0 ");}//...... hundreds of rows // I found it wrong. If there are more than 20 required items, do I have to write it like this all the time! Return validateMessage. ToString ();}
Such code is not highly reusable and inefficient.
We can use features, reflection, and then traverse the attributes and check the features.
First, define a [mandatory] feature class, inherit from the Attribute
/// <Summary> // [required] feature, inherited from Attribute // </summary> public sealed class RequireAttribute: Attribute {private bool isRequire; public bool IsRequire {get {return isRequire ;}} /// <summary> /// constructor /// </summary> /// <param name = "isRequire"> </param> public RequireAttribute (bool isRequire) {this. isRequire = isRequire ;}}
Then, use this custom feature to mark the member attributes of the student class:
/// <Summary> /// Student class /// </summary> public class Student {// <summary> /// name /// </summary> private string name; [Require (true)] public string Name {get {return name;} set {name = value ;}} /// <summary >/// Age /// </summary> [Require (true)] public int Age {get; set ;} /// <summary> /// Address /// </summary> [Require (false)] public string Address {get; set ;} /// <summary> /// gender /// </summary> [Require (true)] public string Sex ;}
Check the attributes of the class through features:
/// <Summary> /// check method, supports generics //</summary> /// <typeparam name = "T"> </typeparam> /// <param name = "instance"> </param> /// <returns> </returns> public static string CheckRequire <T> (T instance) {var validateMsg = new StringBuilder (); // obtain the Type T = typeof (t); var propertyInfos = T. getProperties (); // traverse the attribute foreach (var propertyInfo in propertyInfos) {// check whether the Attribute has the feature RequireAttribute = (RequireAttribute) attribute. getCustomAttribute (propertyInfo, typeof (RequireAttribute); // unspecified, skip if (attribute = null) {continue;} // obtain the attribute data type var type = propertyInfo. propertyType. toString (). toLower (); // obtain the value of this attribute var value = propertyInfo. getValue (instance); if (type. contains ("system. string ") {if (string. isNullOrEmpty (string) value) & attribute. isRequire) validateMsg. append (propertyInfo. name ). append ("cannot be blank "). append (",");} else if (type. contains ("system.int") {if (int) value = 0 & attribute. isRequire) validateMsg. append (propertyInfo. name ). append ("must be greater than 0 "). append (",") ;}} return validateMsg. toString ();}
Perform verification:
static void Main(string[] args) { var obj = new Student() { Name = "" }; Console.WriteLine(CheckRequire(obj)); Console.Read(); }
Result output:
Some people will find that Sex is also marked with [Require (true)]. Why is there no authentication information? This is because Sex does not implement the {get; set;} attribute and GetProperties cannot be obtained.
I 've added several interesting websites to my previous article. You can check them out .......