The following is an ASP tutorial. NET Entity class object assignment to form implementation code, a few days ago a friend told me reflection form assignment to an object method of an entity class, the following is a very good example.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
///<summary>
///Get form values through object settings
///</summary>
Namespace Com.fun
{
public static C Lass setformtomodel<t>
{
///<summary>
///assigns a form to an object
///</summary>
///<para M name= "T" > Entity Objects </param>
///<param name= "Form" > Form collection </param>
public static void GetValue (t T, namevaluecollection form)
{
Type type = T.gettype ();
propertyinfo[] pi = type.getproperties ();
foreach (PropertyInfo p in pi)
{
if (Form[p.name]!= null)
{
P.setvalue (T, Convert.changetype (for M[p.name], p.propertytype), NULL);
}
}
}
<summary>
Assigning an object to a form
</summary>
<param name= "T" > Entity Objects </param>
<param name= "C" > Page objects </param>
public static void SetValue (T t,page page)
{
Type type = T.gettype ();
propertyinfo[] pi = type.getproperties ();
foreach (PropertyInfo p in pi)
{
System.web.ui.htmlcontrols.htmlinputtext Text = Page.findcontrol (p.name) as System.web.ui.htmlcontrols.htmlinputtext;
if (text!= null)
{
Text.value = p.getvalue (t, null). ToString ();
}
}
}
}
}
Call
Mhousereco MH = new Dhousereco (). Getmodel (ID);
Com.fun.setformtomodel<mhousereco>.setvalue (Mh,this.page);
Mhousereco MH = new Mhousereco ();
Com.fun.setformtomodel<mhousereco>.getvalue (MH, This.request.form);