网络软件 系统工具 应用软件 图形图像 多媒体类 免费游戏 安全相关 免费音乐 网页素材 电子书籍 考试考题 建站源码
教育教学 多媒体类 编程开发 操作系统 游戏天地 娱乐天地 简历求职 站长专区 网页设计 安全技术 图形图像 文学驿站
业界资讯 | 图形图像 | 操作系统 | 网络冲浪 | 工具软件 | 办公软件 | 媒体动画 | 精文荟萃 | 认证考试 | 网页设计 | 技术开发 | 专栏
当前位置:热点网络学院编程开发.netDataRow的序列化问题
精品推荐
热点TOP10
·VB.Net中文教程(4) 类别继承(Inheritance)关系
·使用 .NET 框架将现有代码作为 Web 服务提供
·《.net编程先锋C#》第六章 控制语句
·dotNET和VS.NET 学习录像
·Visual Studio.NET Beta2中的团队开发(二)
·C# 和 API
·Visual Studio .NET Enterprise Architect 中基于 Visio 的数据库建模:第三部分
·M.E. Bring .NET CLR Support to C++中文版(下篇)
·Microsoft 发布Rotor,一场Shared Source对Open Source的速度比赛
·LOGIN活动目录
·insert into data.mdb
·ASP.NET 中实现 MD5_HMAC(C#)
·在ASP.NET下用Microsoft Excel进行数据分析与报表
·操作IIS
·ASP.net中的Datagrid自定义分页功能
·编程抑制显示图表示例代码
·文件下载的权限控制(asp.net)
·一周学会C#(结构三)
·重画系列:DataGridColumnStyle之测试代码
·C#2.0 Specification(泛型一)
DataRow的序列化问题
日期:2005年11月10日 作者: 查看:[大字体 中字体 小字体]

在.net里,DataRow类型的对象是不支持序列化的,那么如果在一个需要序列化的对象中含有DataRow类型的字段该怎么办呢?呵呵,幸好Datatable是支持序列化的。因此,我们可以自定义序列化的行为,并在序列化和反序列化的时候用Datatable来对DataRow进行包装和解包。
为了自定义序列化行为,必须实现ISerializable接口。实现这个接口要实现 GetObjectData 方法以及在反序列化对象时使用的特殊构造函数。前者的作用是把该对象要封装的数据加入到系统提供的一个容器中,然后系统会对这些数据进行序列化;后者的作用是把反序列化的数据从容器中取出来,然后显式的赋值给该对象的某一个字段。
如下例所示,应当注意的代码用黑体标出。

using System;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Security.Permissions;

namespace phenix.Dl
{
 /// <summary>
 /// Field 的摘要说明。
 /// </summary>
 [Serializable]
 public class Field:ISerializable
 {
  private string name="";
  private DataRow dr=null;
  private string title="";
  private int index=-1;

  public int Index
  {
   get{return this.index;}
   set{this.index=value;}
  }

  public string Title
  {
   get{return this.title;}
   set{this.title=value;}
  }

  public string FieldName
  {
   get{return this.name;}
   set{this.name=value;}
  }

  public DataRow FieldInfo
  {
   get{return this.dr;}
   set{this.dr=value;}
  }

  public Field()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  protected Field(SerializationInfo info, StreamingContext context)//特殊的构造函数,反序列化时自动调用
  {
   this.name=info.GetString("fieldname");
   this.title=info.GetString("fieldtitle");
   this.index=info.GetInt32("fieldindex");
   DataTable dt=info.GetValue("fieldinfo",new DataTable().GetType()) as DataTable;
   this.dr=dt.Rows[0];
  }

  [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)//序列化时自动调用
  {
   info.AddValue("fieldname", this.name);
   info.AddValue("fieldtitle", this.title);
   info.AddValue("fieldindex", this.index);
   DataTable dt=this.dr.Table.Clone(); //datarow不能同时加入到两个DataTable中,必须先克隆一个
   DataRow row=dt.NewRow();
   row.ItemArray=dr.ItemArray;
   
   dt.Rows.Add(row);
   info.AddValue("fieldinfo",dt,dt.GetType());
  }

 

  public override string ToString()
  {
   return this.name;
  }

 }
}

(出处:http://down.xmsc.com.cn/)

关于我们 | 帮助(?) | 版权声明 | 友情连接 
Copyright 2005-2005 xmsc.com.cn All Rights Reserved.
Powered by:mesky