| Using System; Using System. Collections. Generic; Using System. Linq; Using System. Web; Namespace SyntacticSugar { /// <Summary> /// ** Description: Form help class /// ** Founding Date: /// ** Modification time :- /// ** Prepared by sunkaixuan /// ** Qq: 610262374 welcome to the exchange, joint improvement, naming syntax, and other poorly written areas. You are welcome to give valuable suggestions. /// </Summary> Public class RequestToModel { /// <Summary> /// Submit a form to obtain a single image through reflection /// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned. /// </Summary> Public static T GetSingleForm <T> () where T: new () { T t = SetList <T> (null, 0). Single (); Return t; } /// <Summary> /// Submit a form to obtain a single image through reflection /// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned. /// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param> /// </Summary> Public static T GetSingleForm <T> (string appstr) where T: new () { T t = SetList <T> (appstr, 0). Single (); Return t; } /// <Summary> /// Submit a form to obtain multiple objects through reflection /// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned. /// </Summary> /// <Typeparam name = "type"> </typeparam> /// <Param name = "type"> </param> /// <Returns> </returns> Public static List <T> GetListByForm <T> () where T: new () { List <T> t = SetList <T> (null, 0 ); Return t; } /// <Summary> /// Submit a form to obtain multiple objects through reflection /// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned. /// </Summary> /// <Typeparam name = "type"> </typeparam> /// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param> /// <Returns> </returns> Public static List <T> GetListByForm <T> (string appstr) where T: new () { List <T> t = SetList <T> (appstr, 0 ); Return t; } /// <Summary> /// Submit a form to obtain multiple objects through reflection /// </Summary> /// <Typeparam name = "type"> </typeparam> /// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param> /// <Typeparam name = "index"> the first control in the Form Control, which corresponds to the index number of fields in the class. In special cases, it can be the second and third control. </typeparam> /// <Returns> </returns> Private static List <T> GetListByForm <T> (string appstr, int index) where T: new () { List <T> t = SetList <T> (appstr, index ); Return t; } Private static List <T> SetList <T> (string appendstr, int index) where T: new () { List <T> t = new List <T> (); Try { Var properties = new T (). GetType (). GetProperties (); Var subNum = System. Web. HttpContext. Current. Request [appendstr + properties [index]. Name]. Split (','). Length; For (int I = 0; I <subNum; I ++) { Var r = properties; Var model = new T (); Foreach (var p in properties) { String pval = System. Web. HttpContext. Current. Request [appendstr + p. Name + ""]; If (! String. IsNullOrEmpty (pval )) { Pval = pval. Split (',') [I]; String pptypeName = p. PropertyType. Name; P. SetValue (model, Convert. ChangeType (pval, p. PropertyType), null ); } } T. Add (model ); } } Catch (Exception ex) { Throw ex; } Return t; } } } |