. 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)!