using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data.OleDb; using System.Web; using System.Web.UI.WebControls; using System.Web.UI; namespace Maticsoft.Common { /// <summary> /// 创建标识:Eman 20101224 /// 类描述:只需传入当前page,就可以自动匹配属性和控件ID,反射赋值、取值; /// </summary> public static class ReflectionHelper { /// <summary> /// /// </summary> /// <param name="page"></param> /// <param name="className"></param> /// <param name="operatorMode">add or update</param> public static void ReflectionSave(Page page, string className, string operatorMode, string[,] propertiesNoInPage) { string modelDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Model.dll"; string modelClassName = "Dvte.Model." + className; Assembly assemblyModel = Assembly.LoadFrom(modelDllPath); Type typeMode = assemblyModel.GetType(modelClassName, true, true); object objMode = Activator.CreateInstance(typeMode, true); PropertyInfo property; foreach (Control control in page.Controls) { if ((control.HasControls()) && (control.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")) { foreach (Control contrControl in control.Controls) { switch (contrControl.GetType().ToString()) { case "System.Web.UI.WebControls.TextBox": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); TextBox textBox = page.FindControl(contrControl.ID) as TextBox; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, textBox.Text, property.PropertyType.Name != "Nullable`1"?false:true, property.PropertyType.FullName), null); break; case "System.Web.UI.WebControls.Label": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); Label lable = page.FindControl(contrControl.ID) as Label; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, lable.Text, property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); break; case "System.Web.UI.WebControls.DropDownList": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); DropDownList dropDownList = page.FindControl(contrControl.ID) as DropDownList; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, dropDownList.SelectedValue, property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); break; default: break; } } } } string billDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Bill.dll"; string billClassName = "Dvte.Bll." + className; Assembly assemblyBLL = Assembly.LoadFrom(billDllPath); Type typeBll = assemblyBLL.GetType(billClassName, true, true); object objBLL = Activator.CreateInstance(typeBll, true); MethodInfo method = objBLL.GetType().GetMethod("Add"); if (propertiesNoInPage != null) { for (int i = 0; i < propertiesNoInPage.GetLength(0); i++) { property = objMode.GetType().GetProperty(propertiesNoInPage[i, 0]); property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, propertiesNoInPage[i, 1], property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); } } method.Invoke(objBLL, new object[] { objMode }); } public static object GetValueByConvert(string typeName, string controlValue, bool isNullble, string fullTypeName) { object obj = new object(); if (false == isNullble) { switch (typeName) { case "String": obj = Convert.ToString(controlValue); break; case "Decimal": obj = Convert.ToDecimal(controlValue); break; case "Int32": obj = Convert.ToInt32(controlValue); break; case "DateTime": obj = Convert.ToDateTime(controlValue); break; } } else { if (fullTypeName.IndexOf("System.DateTime") != -1) { obj = Convert.ToDateTime(controlValue); } else if (fullTypeName.IndexOf("System.String") != -1) { obj = Convert.ToString(controlValue); } else if (fullTypeName.IndexOf("System.Decimal") != -1) { obj = Convert.ToDecimal(controlValue); } else if (fullTypeName.IndexOf("System.Int32") != -1) { obj = Convert.ToInt32(controlValue); } } return obj; } public static void ReflectionRead(Page page, string className,string modeID) { string modelDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Model.dll"; string modelClassName = "Dvte.Model." + className; Assembly assemblyModel = Assembly.LoadFrom(modelDllPath); Type typeMode = assemblyModel.GetType(modelClassName, true, true); object objMode = Activator.CreateInstance(typeMode, true); PropertyInfo property; string billDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Bill.dll"; string billClassName = "Dvte.Bll." + className; Assembly assemblyBLL = Assembly.LoadFrom(billDllPath); Type typeBll = assemblyBLL.GetType(billClassName, true, true); object objBLL = Activator.CreateInstance(typeBll, true); MethodInfo method = objBLL.GetType().GetMethod("GetModel"); objMode= method.Invoke(objBLL, new object[] { modeID }); foreach (Control control in page.Controls) { if ((control.HasControls()) && (control.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")) { foreach (Control contrControl in control.Controls) { switch (contrControl.GetType().ToString()) { case "System.Web.UI.WebControls.TextBox": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); TextBox textBox = page.FindControl(contrControl.ID) as TextBox; textBox.Text = Convert.ToString(property.GetValue(objMode,null)); break; case "System.Web.UI.WebControls.Label": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); Label lable = page.FindControl(contrControl.ID) as Label; lable.Text = Convert.ToString(property.GetValue(objMode,null)); break; case "System.Web.UI.WebControls.DropDownList": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); DropDownList dropDownList = page.FindControl(contrControl.ID) as DropDownList; dropDownList.SelectedValue = Convert.ToString(property.GetValue(objMode, null)); break; default: break; } } } } } public static bool ReflectionUpdate(Page page, string className, string modeID) { bool ifUpdated = false; string modelDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Model.dll"; string modelClassName = "Dvte.Model." + className; Assembly assemblyModel = Assembly.LoadFrom(modelDllPath); Type typeMode = assemblyModel.GetType(modelClassName, true, true); object objMode = Activator.CreateInstance(typeMode, true); PropertyInfo property; string billDllPath = AppDomain.CurrentDomain.BaseDirectory + "//bin//Bill.dll"; string billClassName = "Dvte.Bll." + className; Assembly assemblyBLL = Assembly.LoadFrom(billDllPath); Type typeBll = assemblyBLL.GetType(billClassName, true, true); object objBLL = Activator.CreateInstance(typeBll, true); MethodInfo method = objBLL.GetType().GetMethod("GetModel"); objMode = method.Invoke(objBLL, new object[] { modeID }); foreach (Control control in page.Controls) { if ((control.HasControls()) && (control.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")) { foreach (Control contrControl in control.Controls) { switch (contrControl.GetType().ToString()) { case "System.Web.UI.WebControls.TextBox": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); TextBox textBox = page.FindControl(contrControl.ID) as TextBox; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, textBox.Text, property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); break; case "System.Web.UI.WebControls.Label": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); Label lable = page.FindControl(contrControl.ID) as Label; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, lable.Text, property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); break; case "System.Web.UI.WebControls.DropDownList": property = objMode.GetType().GetProperty(contrControl.ID.Split(new char[] { '_' })[1].ToUpper()); DropDownList dropDownList = page.FindControl(contrControl.ID) as DropDownList; property.SetValue(objMode, GetValueByConvert(property.PropertyType.Name, dropDownList.SelectedValue, property.PropertyType.Name != "Nullable`1" ? false : true, property.PropertyType.FullName), null); break; default: break; } } } } method = objBLL.GetType().GetMethod("Update"); ifUpdated = Convert.ToBoolean(method.Invoke(objBLL, new object[] { objMode })); return ifUpdated; } } } 后台调用: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class ProjectForm_ProjectFormDetail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!this.Page.IsPostBack) { methed.BindData(this.ddl_workFormType, "business_name", "business_id", BLL.Business.drp_workforms_businessOthers); methed.BindData(this.ddl_regionID, "REGIONNAME", "REGIONID", "select * from DC_REGION"); Maticsoft.Common.ReflectionHelper.ReflectionRead(this.Page, "DC_PROJECT", Request.QueryString["projectid"]); } } protected void btn_UpdateProject_Click(object sender, EventArgs e) { if (Maticsoft.Common.ReflectionHelper.ReflectionUpdate(this.Page, "DC_PROJECT", Request.QueryString["projectid"])) { Function.Script.AlertClientScript(this.Page, "OperatorSuccess", "操作成功"); } } }