反射应用:一条语句获取表单提交内容
ASP.NET页面提交数据,后台获取需要对属性一项一项赋值,像这样:
Product pro = new Product();
pro.ProductType = ddlProductType.SelectedValue;
pro.Shift = ddlShift.SelectedValue;
pro.LeaderType = txtLeaderType.Text; pro.LeaderName = ddlLeaderName.SelectedValue;
数据少还好,数据多了就不舒服咯,有没有更简单的方法呢?要是能一次获取到所有值就好了。
protected Object GetRequestFormObj(Object obj) { //对每个属性进行赋值 Type t = obj.GetType(); PropertyInfo[] PIList = t.GetProperties(); foreach (PropertyInfo pi in PIList) { string m_requestValue = Request[pi.Name]; string m_fieldType = pi.PropertyType.Name.ToLower(); if (m_requestValue == null) continue; if (m_fieldType.IndexOf("string") != -1) { pi.SetValue(obj, GetSaftValue(m_requestValue), null); } else if (m_fieldType.IndexOf("int") != -1) { pi.SetValue(obj, GetIntValue(m_requestValue), null); } else if (m_fieldType.IndexOf("datetime") != -1) { DateTime dtime; if (!DateTime.TryParse(m_requestValue, out dtime)) { dtime = DateTime.Now; } pi.SetValue(obj, dtime, null); } else if (m_fieldType.IndexOf("boolean") != -1) { bool b_boolean; //针对checkbox类型 if (m_requestValue == null) { b_boolean = false; } else if (m_requestValue == "on" || m_requestValue.ToLower() == "true") { b_boolean = true; } else if (!Boolean.TryParse(m_requestValue, out b_boolean)) { b_boolean = false; } pi.SetValue(obj, b_boolean, null); } else if (m_fieldType.IndexOf("double") != -1) { double ddouble; if (!double.TryParse(m_requestValue, out ddouble)) { ddouble = 0; } pi.SetValue(obj, ddouble, null); } else if (m_fieldType.IndexOf("decimal") != -1) { decimal ddecimal; if (!decimal.TryParse(m_requestValue, out ddecimal)) { ddecimal = 0; } pi.SetValue(obj, ddecimal, null); } else { pi.SetValue(obj, Convert.ChangeType(m_requestValue, pi.PropertyType, CultureInfo.CurrentCulture), null); } } return obj; } public string GetSaftValue(string SValue) { return GetSaftValue(SValue, ""); } public string GetSaftValue(string SValue, string DefaultValue) { if (string.IsNullOrEmpty(SValue)) { return DefaultValue; } if (string.IsNullOrEmpty(SValue) || !IsSafeSqlString(SValue)) { return DefaultValue; } else { return SValue.Trim(); } } public bool IsSafeSqlString(string s) { if (((s == null) || (s == string.Empty)) || (s == "")) { return false; } string input = Regex.Replace(s.Replace("%20", " "), @"\s", " "); string pattern = @"select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|cast\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\' | or "; return !Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase); } public int GetIntValue(string StrValue) { if (IsInt(StrValue)) { return int.Parse(StrValue); } return 0; } public bool IsInt(string str) { return IsInt(str, false); } public bool IsInt(string str, bool isplus) { if (((str == null) || (str == string.Empty)) || (str == "")) { return false; } if (isplus) { return Regex.IsMatch(str, "^[1-9][0-9]*$"); } return Regex.IsMatch(str, "^[0-9]*$"); } public bool IsNotEmpty(string Str) { if ((string.IsNullOrEmpty(Str) || (Str.Length == 0)) || (Str == "")) { return false; } return true; }
核心代码只有几句:
Type t = obj.GetType();//获取对象类型 PropertyInfo[] PIList = t.GetProperties();//获取当前类所有属性 //对每个属性进行赋值 foreach (PropertyInfo pi in PIList) { string m_requestValue = Request[pi.Name];获取表单提交数据 pi.SetValue(obj,m_requestValue,null);//赋值 }
这样做需要有个前提条件,页面控件ID必须与类的属性名完全一致,不然获取不到的。
调用:
Product pro = new Product();
pro= GetRequestFormObj(pro) as Product;
反射反射,程序员的快乐,挺有意思的。
再找找有没有页面加载一次绑定所有属性值的方法,MVC是可以做到的,看看原理去。

浙公网安备 33010602011771号