Traversal gets all the property names of an entity class, and the values of all properties of the class//defines a class first: public class user{public string name {get; set;} public string Gender {get; set;} public string Age {get; set;}} Instantiate the class and assign a value to the property of the materialized pair: User U = New user (), u.name = "Ahbool"; u.gender = "male";//output the value corresponding to all property names and properties of this class Response.Write ( GetProperties (u));//output is: Name:ahbool,gender: Male, age:,//traversal gets the value of the class's properties and properties: public string getproperties<t> (T t) { String tstr = String. Empty; if (t = = null) {return tstr; } system.reflection.propertyinfo[] Properties = T.gettype (). GetProperties (System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties. Length <= 0) {return tstr; } foreach (System.Reflection.PropertyInfo item in properties) {string name = Item. Name; Object value = Item. GetValue (t, NULL); if (item. Propertytype.isvaluetype | | Item. PropertyType.Name.StartsWith ("String")) {tstr + = string. Format ("{0}:{1},", name, value); } else {getProperties (value); }} return tstr;}
C # Get Entity class property names and values