打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
多表单提交

合理利用PropertyInfo.SetValue()

在asp.net开发中,一般都会按最基本的分层来做,这里说的最基本的分层是指实体层(model)、数据操作层、表现层。实体层一般来说都是数据库中表字段的映射。

一个典型的常见的代码如下: view plaincopy to clipboardprint?

 public class Product

 {

public int ID { get; set; }

public string ProductName { get; set; }

 public decimal Price { get; set; } //..其它属性

 }

 public class Product {

 public int ID { get; set; }

 public string ProductName { get; set; }

 public decimal Price { get; set; } //..其它属性

 }

一个操作类: view plaincopy to clipboardprint?

public void AddProduct(Product p)

 { //数据操作 }

public void AddProduct(Product p) { //数据操作 }

 UI层会有一个表单,以供填写相关内容,点击提交后,代码一般如下: view plaincopy to clipboardprint?

Product p = new Product(); p.Name=Request.Form["xxx"]; //.... AddProduct(p); //..... Product p = new Product(); p.Name=Request.Form["xxx"]; //.... AddProduct(p); //.....

如果实体类有20个属性,那我们得赋20(或20以下)次值,如果更多,那就太枯燥无聊了。试想,我们通过反射找到该类的所有属性,然后给这些属性相应进行赋值不就省心许多?

但,如何来实现?首先,我们的表单项的name属性一定要与class的属性名完全一致,然后处理post过来的所有表单项。代码可能如下: view plaincopy to clipboardprint?

 public static void SetObjectPropertyValue(object obj, StringDictionary nv)

 {

 Type type = obj.GetType();

 PropertyInfo[] pi = type.GetProperties();

foreach (PropertyInfo item in pi)

{

 if (nv.ContainsKey(item.Name.ToLower()))

 {

item.SetValue(obj, Convert.ChangeType(nv[item.Name.ToLower()], item.PropertyType, CultureInfo.CurrentCulture), null);

}

}

 }

 public static void SetObjectPropertyValue(object obj, StringDictionary nv)

{

Type type = obj.GetType();

PropertyInfo[] pi = type.GetProperties();

 foreach (PropertyInfo item in pi)

{

 if (nv.ContainsKey(item.Name.ToLower()))

{

 item.SetValue(obj, Convert.ChangeType(nv[item.Name.ToLower()], item.PropertyType, CultureInfo.CurrentCulture), null);

} } }

方法SetObjectPropertyValue就实现对类的属性进行赋值的功能。该方法是反射出obj的所有属性,然后调用PropertyInfo的SetValue方法进行赋值。参数nv是一个StringDictionary 类型的数据,是这样进行传送的: view plaincopy to clipboardprint?

 System.Collections.Specialized.NameValueCollection nameValueCollection = Request.Form; System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();

 foreach (string key in nameValueCollection.Keys)

{

 if (sd.ContainsKey(key) == false)

 sd.Add(key, nameValueCollection[key]);

 }

 Utility.SetObjectPropertyValue(action, sd); //数据操作

 System.Collections.Specialized.NameValueCollection nameValueCollection = Request.Form; System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary();

foreach (string key in nameValueCollection.Keys)

 { if (sd.ContainsKey(key) == false) sd.Add(key, nameValueCollection[key]); } Utility.SetObjectPropertyValue(action, sd); //数据操作

OK,结束。但没有对该方法作过性能上的测试

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# PropertyGrid控件应用心得
模拟实现struts2拦截器原理
hibernate之脏数据检查
编写超级可读代码的15个最佳实践
Java语言基础:内部类
Spring(3)--Lookup Method Injection - flowerkn...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服