Get the object set attribute value through reflection and assign it to another object set
In the method provided by List, a method is provided to convert a List type into another List type, as follows:
// Construct a set of Class 1 TestClass1 test1 = new TestClass1 (); List
Class1 = new List
(); Test1. Name = "lhc"; test1. ID = 2; class1.Add (test1); // construct the set TestClass2 test2 = new TestClass2 (); List
Class2 = new List
(); Class2.Add (test2); // The two sets are converted to each other. class2 = class1.ConvertAll
(New Converter
(C1 => new TestClass2 () {Name = c1.Name}); // specify the conversion rule in convert
Code above: using the ConvertAll method of List, you can convert the two types of sets, but you need to put the Converter's structure and define the conversion rules, the conversion rule in the above Code is a Lambda expression that specifies the ing between attributes.
This method is more practical. However, in special cases, for example, in WCF, the property names of the Data contract and the Model layer object are the same. At this time, if we manually write this conversion rule, it will be very troublesome. Here, we discard this method and use generics to manually write a general method:
# Public static List
ListInputToOutput
(List
ObjTlist) where T: new () where M: new () {// defines the returned List
ListM = new List
(); // Defines the temporary M class variable M TempM = new M (); foreach (T item in objTlist) {TempM = InputToOutput
(Item); ListM. add (TempM); // Add the result set} return ListM ;} # endregion # region single entity conversion process // conversion rules // T represents the object to be converted // M represents the converted object // the object to be converted in the parameter public static M InputToOutput
(T ObjT) where T: new () where M: new () // where M: ICollection
{String tempName = string. empty; // temporarily Save the attribute name // obtain all attributes and values of T // T t = new T (); M Result = new M (); // define the returned value PropertyInfo [] propertys = Result. getType (). getProperties (); // cyclically obtain the attributes of T and its value foreach (PropertyInfo pr in propertys) {tempName = pr. name; // assign the attribute Name to the Temporary Variable PropertyInfo findPro = ObjT. getType (). getProperty (tempName); // obtain the property if (findPro! = Null) // if a value is obtained, execute the value assignment process {if (! Pr. canWrite) continue; // If the attribute cannot be written, Jump Out // value: the parameter does not match ----------- object value = findPro. getValue (ObjT, null); // if it is not empty, the property if (value! = DBNull. Value) pr. SetValue (Result, value, null) ;}} return Result ;}# endregion
The specific idea of this Code is to get the attribute set of M object and find it in T's attribute. If T contains this attribute, assign the value of T to the property of M. If not, skip this step.
The process of dynamically obtaining attribute values can be seen in many cases. However, you should pay special attention to the where constraint, because the type is determined only when the generics are dynamically compiled, however, some attribute methods of the type may be used in the Code. In this case, you must add constraints.