. NET using Protobuffer for serialization and deserialization

Source: Internet
Author: User

. NET using Protobuffer for serialization and deserialization

1. To the official download Protobuf-net.dll, official address: http://code.google.com/p/protobuf-net/

2. Build a console Application

3. Add Class Library: Protobuf-net.dll to the application.

Example code:

Prepare an entity class to be tested (note that both classes and methods are prefixed with the Protobuffer serialization feature):

[Protocontract]    public class Student    {        [Protomember (1)] public        Intstudentid {get; set;}        [Protomember (2)]        Public Stringname {get; set;}        [Protomember (3)]        Public Stringclassname {get; set;}    }

The class is then serialized and deserialized

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) {////////////////////////serialization////////////////////////////// Student stu = new Student () {StudentID = 1, Name = "Zhangsan", Cl             Assname = "Classone"}; if (!               File.exists (Testpath)) {FileStream fs = File.create (testpath,1024, fileoptions.asynchronous); Fs.            Dispose ();            } Console.WriteLine ("Start serialization and export to File ..."); using (Stream s = new FileStream (Testpath,filemode.open, FileAccess.ReadWrite)) {Serializer.ser                Ialize<student> (S, Stu);            S.close ();    } Console.WriteLine ("Serialization Complete");         Deserialize////////////////////////////Console.WriteLine ("Deserialize and output ..."); using (Stream s = new FileStream (Testpath,filemode.open)) {Student St = serializer.deserialize& Lt                Student> (s); Console.WriteLine ("Studentname:" + stu. Name + "/r/n" + "StudentID:" + stu. StudentID + "/r/n" + "ClassName:" + stu.                ClassName);            S.close ();        } console.read (); }   }}

Now consider the case of multiple entities and test serialize a collection :

Class Program {Private Const string testpath = @ "D:/1.txt";             static void Main (string[] args) {////////////////////////serialization////////////////////////////// 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 ("Start serializing and exporting files ..."); using (Stream s = new FileStream (Testpath,filemode.open, FileAccess.ReadWrite)) {Serializer.ser                Ialize<list<student>> (S,stu);            S.close (); } Console.WriteLine ("Sequence");            Deserialize////////////////////////////Console.WriteLine ("Deserialize and output ..."); 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 (); }   }

The above is. NET to use Protobuffer to implement serialization and deserialization of the detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.