C#?二進(jìn)制序列化和反序列化的具體實(shí)現(xiàn)
.NET框架提供了兩種種串行化的方式:
1、是使用BinaryFormatter進(jìn)行串行化;
2、使用XmlSerializer進(jìn)行串行化。
第一種方式提供了一個(gè)簡(jiǎn)單的二進(jìn)制數(shù)據(jù)流以及某些附加的類型信息,而第二種將數(shù)據(jù)流格式化為XML存儲(chǔ)??梢允褂肹Serializable]屬性將類標(biāo)志為可序列化的。如果某個(gè)類的元素不想被序列化,1、可以使用[NonSerialized]屬性來標(biāo)志,2、可以使用[XmlIgnore]來標(biāo)志。
序列化意思指的是把對(duì)象的當(dāng)前狀態(tài)進(jìn)行持久化,一個(gè)對(duì)象的狀態(tài)在面向?qū)ο蟮某绦蛑惺怯蓪傩员硎镜?,所以序列化類的時(shí)候是從屬性讀取值以某種格式保存下來,而類的成員函數(shù)不會(huì)被序列化,.net存在幾種默認(rèn)提供的序列化,二進(jìn)制序列化,xml和json序列化會(huì)序列化所有的實(shí)例共有屬性。
這里簡(jiǎn)單介紹:BinaryFormatter以二進(jìn)制格式序列化和反序列化對(duì)象。
BinaryFormatte序列化:將對(duì)象轉(zhuǎn)化成二進(jìn)制,BinaryFormatte反序列化就是將二進(jìn)制轉(zhuǎn)化為對(duì)象;
命名空間: System.Runtime.Serialization.Formatters;
最常用的兩個(gè)方法:
- Deserialize(Stream) 將指定的流反序列化成對(duì)象
- Serialize(Stream, Object) 將對(duì)象序列化到給定的流
兩個(gè)常用的屬性:
- Serializable 表示可以被序列化
- NonSerializable 屏蔽被序列化
簡(jiǎn)單示例:
namespace Model
{
? ? [Serializable]
? ?public class Config
? ?{ ? ?
? ? ? ?[NonSerialized] ?// 表示下面這個(gè)age字段不進(jìn)行序列化
? ? ? ?private int age{ get; set; }
? ? ? ?public string Language { get; set; }
? ? ? ?public bool IsAutoBackup { get; set; }
? ? ? ?public int BackupTimeForHour { get; set; }
? ? ? ?public string LastTimeRestoreDBFile { get; set; }
? ? ? ?public DateTime? LastAutoBackupDateTime { get; set;}
? ? ? ?public bool IsSupportHis { get; set; }
? ? ? ?//序列化 fileName:文件地址
? ? ? ?public void SaveTo(string fileName)
? ? ? ?{
? ? ? ? ? ?using (MemoryStream ms = new MemoryStream())
? ? ? ? ? ?{
? ? ? ? ? ? ? ?BinaryFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? ? ?formatter.Serialize(ms, this);
? ? ? ? ? ? ? ?File.WriteAllBytes(fileName, ms.ToArray());
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?//反序列化
? ? ? ?public static DSConfig LoadFromFile(string fileName)
? ? ? ?{
? ? ? ? ? ?try
? ? ? ? ? ?{
? ? ? ? ? ? ? ?if (!File.Exists(fileName))
? ? ? ? ? ? ? ? ? ?return null;
? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?byte[] buff = File.ReadAllBytes(fileName);
? ? ? ? ? ? ? ? ? ?using (MemoryStream ms = new MemoryStream(buff))
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?BinaryFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? ? ? ? ? ? ?return (DSConfig)formatter.Deserialize(ms);
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?catch (Exception ex)
? ? ? ? ? ?{
? ? ? ? ? ?}
? ? ? ? ? ?return null;
? ? ? ?}
? ?}
}調(diào)用示例
private const string CONFIGNAME = "b1b4af87-1870-11e9-a31b-8cec4b4fece0.cfg"; public static string ConfigName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Viewer",CONFIGNAME); Config config = Config.LoadFromFile(ConfigName);
//對(duì)Config類里面參數(shù)賦值后保存 Config.SaveTo(ConfigName);
BinaryFormatte序列化,示例二:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Binaryformats
{
internal class Program
{
private static void Main(string[] args)
{
Person p = new Person();
p.Sex = 1;
p.Age = 21;
p.Name = "dfr";
byte[] serBytes = BinaryFormat.Serialize(p); //序列化
Person pp = (Person) BinaryFormat.Deserialize(serBytes); //反序列化,object類轉(zhuǎn)化成自己定義的
Console.WriteLine(pp.Name);
Console.ReadLine();
}
[Serializable]
private class Person //用Serializable做了標(biāo)記,標(biāo)識(shí)可以被序列化
{
private int _age;
[NonSerialized]
private string _name; //用NonSerialized做了標(biāo)記,標(biāo)識(shí)該字段屏蔽序列化
private int _sex;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Sex
{
get { return _sex; }
set { _sex = value; }
}
public int Age
{
get { return _sex; }
set { _sex = value; }
}
}
}
public class BinaryFormat
{
public static byte[] Serialize(Object Urobject) //序列化 返回byte[]類型
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream memory = new MemoryStream();//使用using 可以避免忘記釋放
bf.Serialize(memory, Urobject);
byte[] bytes = memory.GetBuffer();
memory.Close();
return bytes;
//或者采用方法:
using (MemoryStream ms = new MemoryStream(buff))
{
}
}
public static object Deserialize(byte[] bytes) //反序列化,返回object類型的
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream memory = new MemoryStream(bytes);
object ss = bf.Deserialize(memory);
memory.Close();
return ss;
}
}
}到此這篇關(guān)于C# 二進(jìn)制序列化和反序列化的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 二進(jìn)制序列化和反序列化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity實(shí)現(xiàn)多平臺(tái)二維碼掃描
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)多平臺(tái)二維碼掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法
這篇文章主要介紹了C#判斷當(dāng)前程序是否通過管理員運(yùn)行的方法,可通過非常簡(jiǎn)單的系統(tǒng)函數(shù)調(diào)用實(shí)現(xiàn)對(duì)當(dāng)前程序是否通過管理員運(yùn)行進(jìn)行判定,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
C#中dictionary如何根據(jù)索引值獲取Key值
這篇文章主要介紹了C#中dictionary如何根據(jù)索引值獲取Key值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
c#實(shí)現(xiàn)無標(biāo)題欄窗口的拖動(dòng)
本篇文章是對(duì)c#中實(shí)現(xiàn)無標(biāo)題欄窗口拖動(dòng)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
c#實(shí)現(xiàn)用SQL池,多線程定時(shí)批量執(zhí)行SQL語句的方法
構(gòu)建SQL池,分離業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層,讓業(yè)務(wù)邏輯層從低效的數(shù)據(jù)庫操作解脫,以提高系統(tǒng)整體性能2013-10-10

