国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

DataRow的序列化問題

2024-07-21 02:23:02
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!
在.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;
}

}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇西县| 齐河县| 钟山县| 扬州市| 阳东县| 外汇| 修水县| 三台县| 永年县| 新干县| 岳阳县| 万山特区| 监利县| 务川| 临漳县| 灵石县| 罗江县| 抚州市| 安陆市| 永昌县| 格尔木市| 都匀市| 通州市| 临武县| 济南市| 商河县| 江达县| 莱阳市| 晴隆县| 兴义市| 江都市| 海原县| 阿克陶县| 武安市| 辽阳市| 宜兰市| 赞皇县| 辉县市| 弥渡县| 固阳县| 天气|