您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么使用C#序列化與反序列化進(jìn)行版本控制”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么使用C#序列化與反序列化進(jìn)行版本控制”吧!
當(dāng)涉及到跨進(jìn)程甚至是跨域傳輸數(shù)據(jù)的時(shí)候,我們需要把對(duì)象序列化和反序列化。
首先可以使用Serializable特性。
[Serializable] public class Person { public string _firstName; public string _secondName; //序列化 [OnSerializing] internal void OnSerializing(StreamingContext context) { _firstName = _firstName.ToUpper(); _secondName = _secondName.ToUpper(); } //反序列化 [OnDeserialized] internal void OnDeserialized(StreamingContext context) { _firstName = _firstName.ToLower(); _secondName = _secondName.ToLower(); } }
當(dāng)然,也可以實(shí)現(xiàn)ISerializable接口。在接口方法GetObjectData完成序列化,把信息保存到SerializationInfo中,在構(gòu)造函數(shù)中反序列化,把信息從SerializationInfo中讀取出來(lái)。
[Serializable] public class Person : ISerializable { public string _firstName; public string _secondName; public Person() { } //反序列化 public Person(SerializationInfo si, StreamingContext context) { _firstName = si.GetString("first").ToLower(); _secondName = si.GetString("second").ToLower(); } //序列化 public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("first", _firstName.ToUpper()); info.AddValue("second",_secondName.ToUpper()); } }
下面的類,描述了對(duì)象的第一個(gè)版本。
[Serializable] public class SomeClass : ISerializable { private int a; public SomeClass(){} public SomeClass(SerializationInfo info, StreamingContext context) { a = info.GetInt32("myval"); } public void GetObjectData(SerializationInfo, StreamingContext context) { info.AddValue("VERSION",1); info.AddValue("myval", a); } }
現(xiàn)在SomeClass要增加一個(gè)私有字段,成為版本二。
[Serializable] public class SomeClass : ISerializable { private int a; private string b; public SomeClass(){} public SomeClass(SerializationInfo info, StreamingContext context) { int VERSION = info.GetInt32("VERSION"); a = info.GetInt32("a"); if(VERSION > 1) { b = info.GetString("another"); } else { b = "默認(rèn)值"; } } public void GetObjectData(SerializationInfo, StreamingContext context) { info.AddValue("VERSION",2); info.AddValue("myval", a); info.AddValue("another", b); } }
如果一個(gè)集合對(duì)象需要實(shí)現(xiàn)序列化和反序列化,那么集合元素對(duì)象必須可以被序列化和反序列化,并且集合元素對(duì)象中的對(duì)象屬性也必須可以被序列化和反序列化,依此類推。
[Serializable] public class Car : ISerializable { private string _model; private int _year; private Owner _owner; public Car() { } public Car(SerializationInfo info, StreamingContext context) { this._model = (string) info.GetValue("Model", typeof (string)); this._year = (int) info.GetValue("Year", typeof (int)); this._owner = (Owner) info.GetValue("Owner", typeof (Owner)); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Model",this._model); info.AddValue("Year", this._year); info.AddValue("Owner", this._owner); } } [Serializable] public class Owner : ISerializable { private string _name; public Owner() { } public Owner(SerializationInfo info, StreamingContext context) { this._name = (string) info.GetValue("Name", typeof (string)); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", this._name); } }
以上,我們想序列化Car的集合到文件中,再反序列化Cars的集合讀取出來(lái)。那么,我們必須讓Car可以被序列化和反序列化,Car的屬性對(duì)象Owner也必須可以被序列化和反序列化。
接著,用來(lái)封裝Car集合的一個(gè)類。
[Serializable] public class CarsList : ISerializable { private List<Car> _cars; public List<Car> Cars { get { return this._cars; } set { this._cars = value; } } public CarsList() { } public CarsList(SerializationInfo info, StreamingContext context) { this._cars = (List<Car>) info.GetValue("Cars", typeof (List<Car>)); } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Cars", this._cars); } }
針對(duì)CarsList寫一個(gè)序列化和反序列化的幫助類。
public class SerializerHelper { public SerializerHelper() { } //序列化 public void SerializeObject(string fileName, CarsList carsList) { //根據(jù)文件名打開流 Stream stream = File.Open(fileName, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); //把對(duì)象序列化到流中 bFormatter.Serialize(stream,carsList); stream.Close(); } //反序列化 public CarsList DeserializeObject(string fileName) { CarsList carsList; //根據(jù)文件名打開流 Stream stream = File.Open(fileName, FileMode.Open); BinaryFormatter bfFormatter = new BinaryFormatter(); carsList = (CarsList)bfFormatter.Deserialize(stream); stream.Close(); return carsList; } }
在客戶端按如下調(diào)用:
static void Main(string[] args) { List<Car> cars = new List<Car>(); CarsList carsList = new CarsList(); carsList.Cars = cars; SerializerHelper serializerHelper = new SerializerHelper(); serializerHelper.SerializeObject(@"temp.txt", carsList); carsList = serializerHelper.DeserializeObject(@"temp.txt"); cars = carsList.Cars; }
到此,相信大家對(duì)“怎么使用C#序列化與反序列化進(jìn)行版本控制”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。