日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

DataRow的序列化问题

發(fā)布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataRow的序列化问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? 來源:CSDN? 作者:kroll

在.net里,DataRow類型的對象是不支持序列化的,那么如果在一個需要序列化的對象中含有DataRow類型的字段該怎么辦呢?呵呵,幸好Datatable是支持序列化的。因此,我們可以自定義序列化的行為,并在序列化和反序列化的時候用Datatable來對DataRow進(jìn)行包裝和解包。
為了自定義序列化行為,必須實(shí)現(xiàn)ISerializable接口。實(shí)現(xiàn)這個接口要實(shí)現(xiàn) GetObjectData方法以及在反序列化對象時使用的特殊構(gòu)造函數(shù)。前者的作用是把該對象要封裝的數(shù)據(jù)加入到系統(tǒng)提供的一個容器中,然后系統(tǒng)會對這些數(shù)據(jù)進(jìn)行序列化;后者的作用是把反序列化的數(shù)據(jù)從容器中取出來,然后顯式的賦值給該對象的某一個字段。
如下例所示,應(yīng)當(dāng)注意的代碼用黑體標(biāo)出。

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: 在此處添加構(gòu)造函數(shù)邏輯
???//
??}

??protected Field(SerializationInfo info, StreamingContext context)//特殊的構(gòu)造函數(shù),反序列化時自動調(diào)用
??{
???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)//序列化時自動調(diào)用
??{
???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;
??}

?}
}

<script event=onload for=window type=text/javascript>ImgLoad(document.getElementById("BodyLabel"));</script>

總結(jié)

以上是生活随笔為你收集整理的DataRow的序列化问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。