ddc
联网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
网站建设 域名注册 网站建设 虚拟主机 广州网站设计 域名注册 广州网站建设 上海网站建设 虚拟主机 广州网页设计 虚拟主机 域名注册 acg王国 ACG玩家 品牌设计 上海网站建设
求创科技
网站建设
中国互联
素材出售
中国设计秀
中资源
当前位置:网络学院首页 >> 编程开发 >> .net >> Asp.net下的对象成员数据绑定器实现

Asp.net下的对象成员数据绑定器实现 (3)

来源:中国设计秀    作者:    点击:25     加入收藏    发表评论
0
顶一下

通过ConverterAttribute可以方便制定粒度更小的配置
  
  private byte[] mFileStream;
  
   [Converter(typeof(FileStreamConverter),"IconPhoto")]
  
   public byte[] FileStream
  
   {
  
   get
  
   {
  
   return mFileStream;
  
   }
  
   set
  
   {
  
   mFileStream = value;
  
   }
  
   }
  
  以上定义可以上传文件流转成byte[]到FileStream属性中。
  
  功能集成实现
   现在就把所有东西集成起来,满足目的的要求。
  
   public object Bind(System.Collections.Specialized.NameValueCollection values, string prefix)
  
   {
  
   object newobj = Activator.CreateInstance(ObjectType);
  
   if (prefix == null)
  
   prefix = "";
  
   object value;
  
   foreach (PropertyInfo item in Properties)
  
   {
  
   value = values[prefix + "." + item.Name];
  
   if(value == null)
  
   value = values[prefix + "_" + item.Name];
  
   if(value == null)
  
   value = values[prefix + item.Name];
  
   BindProperty(newobj, item, (string)value);
  
   }
  
   return newobj;
  
   }
  
   private void BindProperty(object obj, PropertyInfo property, string value)
  
   {
  
   IStringConverter stringconver;
  
   object nvalue;
  
   bool confirm = false;
  
   Object[] cas = property.GetCustomAttributes(typeof(ConverterAttribute), true);
  
  
  
   if (cas.Length > 0)
  
   {
  
   nvalue = ((ConverterAttribute)cas[0]).ConvertTo(value, out confirm);
  
   if (confirm)
  
   mPropertiesHandle[property].SetValue(obj, nvalue);
  
  
  
   }
  
   else
  
   {
  
   if (ConverterFactory.Converters.ContainsKey(property.PropertyType))
  
   {
  
   stringconver = ConverterFactory.Converters[property.PropertyType];
  
   nvalue = stringconver.ConvertTo(value, out confirm);
  
   if (confirm)
  
   mPropertiesHandle[property].SetValue(obj, nvalue); 
   
   }
  
   } 
   
   }
  
  因为Web提交的数据几乎可以通过HttpRequest.Params得到,只需要根据属性名称和相关前缀进行匹配查找就可以了。这里实现的匹配方式并不理想,其实可以在相关page第一次请求就可以分析到关系存在IDictionary中,后期直接使用就可以了。 
   
   以上功能是在编写一个MVC组件的数据绑定功能,其实完全可以移植传统的WebForm下工作;有更好想法的朋友请多提交意见。

[1] [2] [3]
2007-03-09 17:15:00    出处:
Google
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口