The following is the implementation code for assigning a value to a form for an object class in asp.net. A friend told me a few days ago that the reflection form assigned a value to an object method of an object class. Below is a very good example.
The following is an asp tutorial. net object assignment to form implementation code, a friend told me a few days ago reflection form assignment to an object method of an object class, below 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.html controls;
/// <Summary>
/// Obtain the form value through object settings
/// </Summary>
Namespace com. fun
{
Public static class setformtomodel <t>
{
/// <Summary>
/// Grant the form to the object
/// </Summary>
/// <Param name = "t"> Object </param>
/// <Param name = "form"> form set </param>
Public static void getvalue (t, namevaluecollection form)
{
Type = t. gettype ();
Propertyinfo [] pi = type. getproperties ();
Foreach (propertyinfo p in pi)
{
If (form [p. name]! = Null)
{
P. setvalue (t, convert. changetype (form [p. name], p. propertytype), null );
}
}
}
/// <Summary>
/// Assign the object to the form
/// </Summary>
/// <Param name = "t"> Object </param>
/// <Param name = "c"> Page Object </param>
Public static void setvalue (t, page)
{
Type = t. gettype ();
Propertyinfo [] pi = type. getproperties ();
Foreach (propertyinfo p in pi)
{
System.web.ui.htmlcontrols.html inputtext text = page. findcontrol (p. name) as system.web.ui.htmlcontrols.html inputtext;
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 );