最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#?二進(jìn)制序列化和反序列化的具體實(shí)現(xiàn)

 更新時(shí)間:2023年06月14日 09:34:50   作者:^@^lemon?tea^@^  
本文主要介紹了C#?二進(jìn)制序列化和反序列化的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

.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)文章

最新評(píng)論

伊川县| 衡东县| 阿克| 富源县| 夏邑县| 藁城市| 云梦县| 渝中区| 扎赉特旗| 灵台县| 开鲁县| 治多县| 英德市| 东宁县| 麻城市| 民乐县| 克东县| 辽阳县| 尉犁县| 壤塘县| 将乐县| 马龙县| 青岛市| 拉萨市| 金溪县| 柯坪县| 青岛市| 株洲市| 阳山县| 额敏县| 清水县| 上栗县| 冕宁县| 上饶县| 阳东县| 长武县| 东乡县| 鹤壁市| 肃北| 永登县| 伽师县|