//new version, can be globa.ascx inside set development mode//to facilitate debugging errors//form to entity object version V2 Public classHttprequesthelper {/// <summary> ///development mode, set to development mode, throw an exception, see the properties of the error, and type/// </summary> Public Static BOOLDevMode {Get;Set; } #regionForm conversion to solid model/// <summary> ///form conversion to solid model///usage: 1, INSERT, first new object incoming;///2, update, first isolated from the database this object incoming/// </summary> /// <param name= "obj" >Generic Object</param> /// <returns></returns> Public StaticT convertform2model<t>(T obj) {if(NULL==obj) {obj=default(T); Obj= activator.createinstance<t>(); } Type Type=typeof(T); foreach(varPinchtype. GetProperties ()) {stringresult =Httpcontext.current.request.params[p.name]; if(!string. Isnullorwhitespace (Result)) {Try{p.setvalue (obj, Convert.changetype (result, P.propertytype),NULL); } Catch(Exception ex) {if(DevMode) {Throw NewException ("type conversion error, variable:"+ P.name +", type:"+ P.propertytype +", error message:"+Ex. Message); } Else { //logging .... } } } } returnobj; } #endregion }
/////////////////////do you have any good ideas, please give a suggestion//[email protected]////////////////// Public classHttprequesthelper<t>whereT:class,New() { #regionForm conversion to solid model/// <summary> ///form conversion to solid model///usage: 1, INSERT, first new object incoming;///2, update, first isolated from the database this object incoming/// </summary> /// <param name= "obj" >Generic Object</param> /// <returns></returns> Public StaticT Convertform2model (t obj=NULL) { if(NULL==obj) {obj=default(T); Obj= activator.createinstance<t>(); } Type Type=typeof(T); foreach(varPinchtype. GetProperties ()) {stringresult =Httpcontext.current.request.params[p.name]; if(!string. Isnullorwhitespace (Result)) {Try{p.setvalue (obj, Convert.changetype (result, P.propertytype),NULL); } Catch(Exception ex) {Throw NewException ("type conversion error, variable:"+ P.name +", type:"+ p.propertytype+", error message:"+Ex. Message); } } } returnobj; } #endregion }
often encountered int age=convert.toint32 (THIS.TXTAGE.TEXT);
This kind of egg-ache code, close-up method.
The exception is thrown in the hope of knowing that the conversion failed, what the malformed property is, and that it is easy to debug.
ASP. From form to entity class