.NET中使用Protobuffer 實現序列化和還原序列化的詳解

來源:互聯網
上載者:User

.NET中使用Protobuffer實現序列化和還原序列化

1. 到官方下載protobuf-net.dll,官方地址:http://code.google.com/p/protobuf-net/

2. 建一個控制台應用程式

3. 添加類庫:protobuf-net.dll到應用程式。

範例程式碼:

準備一個要測試的實體類(注意類和方法都要加上protoBuffer序列化的特性):

 [ProtoContract]    public class Student    {        [ProtoMember(1)]        public intStudentId { get; set; }        [ProtoMember(2)]        public stringName { get; set; }        [ProtoMember(3)]        public stringClassName { get; set; }    }

然後對這個類進行序列化還原序列化

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ProtoBuf;using ProtoBufferDemo.Entity;using System.IO; namespace ProtoBufferDemo{   class Program   {        private const string TestPath = @"D:/1.txt";        static void Main(string[] args)        {            ////////////////////////序列化//////////////////////////////             Student stu = new Student()            {                StudentId = 1,                Name = "zhangsan",                ClassName = "classOne"            };             if (!File.Exists(TestPath))            {               FileStream fs = File.Create(TestPath,1024, FileOptions.Asynchronous);               fs.Dispose();            }             Console.WriteLine("開始序列化並匯出到檔案...");            using (Stream s = new FileStream(TestPath,FileMode.Open ,FileAccess.ReadWrite))            {                Serializer.Serialize<Student>(s, stu);                s.Close();            }            Console.WriteLine("序列化完畢");             //////////////////////還原序列化////////////////////////////             Console.WriteLine("還原序列化並輸出...");            using (Stream s = new FileStream(TestPath,FileMode.Open))            {                Student st = Serializer.Deserialize<Student>(s);                Console.WriteLine("studentName:"+ stu.Name + "/r/n" +                                  "studentId:"+ stu.StudentId + "/r/n" +                                  "className:" + stu.ClassName);                s.Close();            }             Console.Read();        }   }}

現在考慮一下多個實體的情況,測試一下序列化一個集合

class Program   {        private const string TestPath = @"D:/1.txt";        static void Main(string[] args)        {            ////////////////////////序列化//////////////////////////////             List<Student> stu = new List<Student>()            {                new Student(){                StudentId = 1,                Name = "zhangsan",                ClassName = "classOne"},                 new Student(){StudentId = 2,                Name = "lisi",                ClassName = "classTwo"}            };             if (!File.Exists(TestPath))            {                FileStream fs = File.Create(TestPath,1024, FileOptions.Asynchronous);                fs.Dispose();            }             Console.WriteLine("開始序列化並匯出檔案...");            using (Stream s = new FileStream(TestPath,FileMode.Open, FileAccess.ReadWrite))            {                Serializer.Serialize<List<Student>>(s,stu);                s.Close();            }            Console.WriteLine("序列化完畢");             //////////////////////還原序列化////////////////////////////             Console.WriteLine("還原序列化並輸出...");            using (Stream s = new FileStream(TestPath,FileMode.Open))            {                List<Student> sl = Serializer.Deserialize<List<Student>>(s);                 foreach (var student in sl)                {                    Console.WriteLine("studentName:"+ student.Name + "/r/n" +                                "studentId:" + student.StudentId + "/r/n" +                                "class Name:" + student.ClassName);                }                               s.Close();            }             Console.Read();        }   }

以上就是.NET中使用Protobuffer 實現序列化和還原序列化的詳解的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!


  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.