In web development, the more commonly used data transfer format is JSON key-value pair string format, whereas in the general ORM full-mapping or semi-mapping technology, the data obtained from the backend is often enumerable or list<t> type data, so a general class is made, Implements the conversion of data of type list<t> to JSON-formatted data. The code is as follows:
First, a base class reflect based on reflect is established to implement an assignment or value operation on a property named a given string:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Collections;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Reflection;7 8 namespacereflectionlearning9 {Ten classReflect One { A PrivateType _type; - Private Object_obj; - PrivatePropertyInfo _propertyinfo; the - PublicReflect (Objectobj) - { - This. _obj =obj; +_type =obj. GetType (); - } + A /// <summary> at ///Get property values by reflection, passing in property names - /// </summary> - /// <param name= "name" ></param> - /// <returns></returns> - Public ObjectGetValue (stringName) - { in_propertyinfo =_type. GetProperty (Name); - Objectresult = _propertyinfo.getvalue (_obj,NULL); to returnresult; + } - the /// <summary> * ///Pass in Property name, property value, complete property assignment Operation $ /// </summary>Panax Notoginseng /// <param name= "name" ></param> - /// <param name= "value" ></param> the Public voidSetValue (stringName,Objectvalue) + { A_propertyinfo =_type. GetProperty (Name); the_propertyinfo.setvalue (_obj,value,NULL); + } - } $}
In a generic class, a reference is made to the reflect class, and then a key-value pair is transformed by traversing each of the properties of each T class in list<t>:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Data;5 usingSystem.Text;6 7 namespacereflectionlearning8 {9 classvalueturningTen { One PrivateReflect Reflect; A Private string[] parms; - PrivateDataTable DT; - PrivateList<type>resultlist; the - //with the data source DT, the attribute list parms, the original list (where elements are defined but not instantiated) Outlist - //Initialize this class - PublicValueturning (DataTable Dt,string[] Parms, list<type>outlist) + { -DT =Dt; +Parms =Parms; AResultlist =outlist; at beginturning (); - getjsonfromlist (resultlist); - } - - /// <summary> - ///iterate through the elements in the list, assigning their properties to the query results in DT in /// </summary> - Public voidbeginturning () to { + Objectobj; - //iterate through every element in a list the for(inti =0; i < dt. Rows.Count; i++) * { $ //accesses the current element to initialize the reflect reflection classPanax Notoginsengobj =Resultlist[i]; -reflect =NewReflect (obj); the + //iterate through each property in this element and assign a value to it A for(intj =0; J < Parms. Length; J + +) the { + //Setting Properties - reflect. SetValue (Parms[j], dt. ROWS[I][PARMS[J]]); $ } $ - tmpreset (obj); - } the } - Wuyi /// <summary> the ///Convert to JSON string with elements in resultlist as data sources - /// </summary> Wu /// <returns>JSON string</returns> - Public stringGetjsonfromlist (list<type>resultlist) About { $StringBuilder Strbuilder =NewStringBuilder (); - if(Resultlist.count <=0) - { - return ""; A } + Else the { - intnum=Resultlist.count; $ intindex =0; the Objectobj; the for(; index < num; index++) the { theobj =Resultlist[index]; -reflect =NewReflect (obj); in stringParm =Parms[index]; theStrbuilder.append ("{"); the strbuilder.append (Couple (parm, reflect. GetValue (Parm). ToString ())); AboutStrbuilder.append ("}"); the if(Index! = num-1) theStrbuilder.append (","); the tmpreset (obj); + } - returnstrbuilder.tostring (); the }Bayi } the the //initialization of temporary objects - Private voidTmpreset (Objectobj) - { theobj =NULL; thereflect =NULL; the } the - //Convert Key-value pairs to "str1": "str2" Form the Public stringCouplestringSTR1,stringstr2) the { the return("\""+ str1 +"\":\""+ str2 +"\"");94 } the } the}
Convert a list generic data source to a JSON string based on reflect