国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
}

}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阆中市| 泽州县| 宜良县| 岳阳县| 怀集县| 勃利县| 蒙自县| 井陉县| 芒康县| 焉耆| 栾川县| 枣强县| 台南县| 高雄县| 龙山县| 安丘市| 昌乐县| 攀枝花市| 施秉县| 开化县| 闻喜县| 德兴市| 太白县| 广州市| 商水县| 彭水| 建昌县| 临朐县| 壤塘县| 盐池县| 鄂州市| 江北区| 大埔县| 绥化市| 剑川县| 修水县| 昭通市| 班戈县| 常德市| 镇江市| 昭通市|