C#?二進(jìn)制序列化和反序列化的具體實(shí)現(xiàn)
.NET框架提供了兩種種串行化的方式:
1、是使用BinaryFormatter進(jìn)行串行化;
2、使用XmlSerializer進(jìn)行串行化。
第一種方式提供了一個(gè)簡單的二進(jìn)制數(shù)據(jù)流以及某些附加的類型信息,而第二種將數(shù)據(jù)流格式化為XML存儲(chǔ)??梢允褂肹Serializable]屬性將類標(biāo)志為可序列化的。如果某個(gè)類的元素不想被序列化,1、可以使用[NonSerialized]屬性來標(biāo)志,2、可以使用[XmlIgnore]來標(biāo)志。
序列化意思指的是把對象的當(dāng)前狀態(tài)進(jìn)行持久化,一個(gè)對象的狀態(tài)在面向?qū)ο蟮某绦蛑惺怯蓪傩员硎镜模孕蛄谢惖臅r(shí)候是從屬性讀取值以某種格式保存下來,而類的成員函數(shù)不會(huì)被序列化,.net存在幾種默認(rèn)提供的序列化,二進(jìn)制序列化,xml和json序列化會(huì)序列化所有的實(shí)例共有屬性。
這里簡單介紹:BinaryFormatter以二進(jìn)制格式序列化和反序列化對象。
BinaryFormatte序列化:將對象轉(zhuǎn)化成二進(jìn)制,BinaryFormatte反序列化就是將二進(jìn)制轉(zhuǎn)化為對象;
命名空間: System.Runtime.Serialization.Formatters;
最常用的兩個(gè)方法:
- Deserialize(Stream) 將指定的流反序列化成對象
- Serialize(Stream, Object) 將對象序列化到給定的流
兩個(gè)常用的屬性:
- Serializable 表示可以被序列化
- NonSerializable 屏蔽被序列化
簡單示例:
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);
//對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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決
本文主要介紹了Unity2021發(fā)布WebGL與網(wǎng)頁交互問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#定時(shí)器實(shí)現(xiàn)自動(dòng)執(zhí)行的方法
這篇文章主要介紹了C#定時(shí)器實(shí)現(xiàn)自動(dòng)執(zhí)行的方法,實(shí)例分析了C#定時(shí)器參數(shù)的設(shè)置及方法的調(diào)用與實(shí)現(xiàn),需要的朋友可以參考下2015-01-01
C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條效果
這篇文章主要介紹了基于C#實(shí)現(xiàn)炫酷啟動(dòng)圖-動(dòng)態(tài)進(jìn)度條 效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
C#實(shí)現(xiàn)同Active MQ通訊的方法
這篇文章主要介紹了C#實(shí)現(xiàn)同Active MQ通訊的方法,簡單分析了Active MQ的功能及C#與之通訊的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
C#使用iTextSharp設(shè)置PDF所有頁面背景圖功能實(shí)例
這篇文章主要介紹了C#使用iTextSharp設(shè)置PDF所有頁面背景圖功能,實(shí)例分析了C#使用iTextSharp設(shè)置PDF頁面背景圖的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

