How does an ASP. NET WebForm Form implement the "Automatic Assembly" effect of MVC?
We know ASP. net mvc has a powerful point: when the Form is submitted to the action, the Form parameters can be directly assembled into the parameter object of the action, such as the action Method Register (UserModel userModel) {.............................} when submitting a form, the fields in the form are automatically encapsulated into the corresponding UserModel field. Can the fields in the WebForm be changed? Because every time we get data, good programmers should learn code encapsulation, code reuse, and repetitive work. We can actually use reflection to instantiate objects (automatic assembly) beijing Alliance network www.bamn.cn is good. There is not much nonsense .... pageload is simple.
Protected void Page_Load (object sender, EventArgs e) {if (! IsPost () {InitPage (); // The first access to the rendering page} else {UserModel userModel = AssembleModel <UserModel> (base. valueCollection );}}
The key is the AssembleModel method in the base class. In the base class, we first obtain the context parameter IT404 protected NameValueCollection valueCollection = HttpContext. current. request. params; the base class is very simple. It stores the submitted parameters of the context to valueCollection and then looks at the AssembleModel method. This is a generic method.
/// <Summary> /// obtain the attributes of the class through reflection /// </summary> /// <param name = "type"> </param> // <returns> </returns> protected PropertyInfo [] GetPropertyInfoArray (Type type) {PropertyInfo [] props = null; try {object obj = Activator. createInstance (type); props = type. getProperties (BindingFlags. public | BindingFlags. instance);} catch (Exception ex) {} return props ;} /// <summary> /// automatically assemble according to NameValueCollection /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "valueCollection"> </param> // <returns> </returns> protected T AssembleModel <T> (NameValueCollection valueCollection) {PropertyInfo [] propertyInfoList = GetPropertyInfoArray (typeof (T); object obj = Activator. createInstance (typeof (T), null); // create a specified type of instance foreach (string key in valueCollection. keys) // value of all contexts {foreach (var PropertyInfo in propertyInfoList) // all object attributes {if (key. toLower () = PropertyInfo. name. toLower () {PropertyInfo. setValue (obj, valueCollection [key], null); // assign a value to the object }}return (T) obj ;}
It is easy to traverse the parameters, and then traverse the common attributes of the Object Class Using Reflection, then match and assign values based on the name. Therefore, in the future, we only need one code to automatically assemble the UserModel userModel = AssembleModel <UserModel> (base. valueCollection); well, thank you for reading this article. I hope it will help you. In addition, this blog will share some original video tutorials with you. Thank you for your support.