C # using Protobuf

Source: Internet
Author: User

How to use C # PROTOBUF

Introduction
Protobuf is an open-source serialization framework provided by Google, similar to the data representation language of Xml,json.
Supports multiple programming languages, now: Java, C #, C + +, Go and Python.
Binary-based, so much more efficient and short than traditional XML representations
As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.
Use

1.: HTTP://CODE.GOOGLE.COM/P/PROTOBUF/DOWNLOADS/2, Proto file format

The package corresponds to a namespace in C #
Required properties of the corresponding class
Optional create a property with a default value, set the default value by [Default=xxx], and do not add the default to be empty. If string defaults to "", int defaults to 0
Enum Creation Enumeration
Message creating a custom class or inner class
Repeated corresponding list list data
Proto Data type:

Example:

package    Test;message person {required string name=1; Required int32 id=2; optional  String Email=3; enum Phonetype {mobile=0; Home=1; Work=2;} message PhoneNumber {required string Number=1; Optional phonetype type=2 [ Default=home]; } repeated PhoneNumber Phone=4;}          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

Proto File editing commands:
Protogen-i:input.proto-o:output.cs
Protogen-i:input.proto-o:output.xml-t:xml
Protogen-i:input.proto-o:output.cs-p:datacontract-q
Protogen-i:input.proto-o:output.cs-p:observable=true

Files after conversion:

//------------------------------------------------------------------------------<auto-generated>This code is generated by a tool.//Changes to this file may cause incorrect behavior and would be lost ifThe code is regenerated.</auto-generated>//------------------------------------------------------------------------------Generated from:input/test.protonamespace input.test{[Global::system.serializable, Global:: Protobuf.protocontract (name=@ "Person")]PublicPartialClass Person:global::P rotobuf.iextensible {PublicPerson () {}Privatestring _name; [Global::P Rotobuf.protomember (1, isrequired =True, Name=@ "Name", DataFormat = global::P rotoBuf.DataFormat.Default)]PublicString Name {get {return _name; }set {_name =Value } }Privateint _id; [Global::P Rotobuf.protomember (2, isrequired =True, Name=@ "id", DataFormat = global::P rotoBuf.DataFormat.TwosComplement)]Publicint ID {get {return _id; }set {_id =Value } }PrivateString _email =""; [Global::P Rotobuf.protomember (3, isrequired =False, Name=@ "Email", DataFormat = global::P rotoBuf.DataFormat.Default)] [Global::system.componentmodel.defaultvalue ("")]PublicString Email {get {return _email; }set {_email =Value } }PrivateReadOnly global::system.collections.generic.list<person.phonenumber> _phone =New Global::system.collections.generic.list<person.phonenumber> (); [Global::P Rotobuf.protomember (4, Name=@ "Phone", DataFormat = global::P rotoBuf.DataFormat.Default)]Public global::system.collections.generic.list<person.phonenumber> Phone {get {return _phone; }} [Global::system.serializable, Global::P rotobuf.protocontract (name=@ "PhoneNumber")]PublicPartialClass Phonenumber:global::P rotobuf.iextensible {PublicPhoneNumber () {}PrivateString _number; [Global::P Rotobuf.protomember (1, isrequired =True, Name=@ "Number", DataFormat = global::P rotoBuf.DataFormat.Default)]PublicString Number {get {return _number; }set {_number =Value } }Private Person.phonetype _type = Person.PhoneType.HOME; [Global::P Rotobuf.protomember (2, isrequired =False, Name=@ "Type", DataFormat = global::P rotoBuf.DataFormat.TwosComplement)] [Global::system.componentmodel.defaultvalue ( Person.PhoneType.HOME)]Public Person.phonetype Type {get {return _type; }set {_type =Value } }Private global::P rotobuf.iextension extensionobject; Global::P rotobuf.iextension Global::P rotoBuf.IExtensible.GetExtensionObject (BOOL createifmissing) {Return Global::P rotoBuf.Extensible.GetExtensionObject (Ref extensionobject, createifmissing); }} [Global::P rotobuf.protocontract (name=@ "Phonetype")]public enum Phonetype {[Global::P rotobuf.protoenum (name=@ "mobile", value=0)] MOBILE = 0, [Global:: Protobuf.protoenum (name=@ "Home", value=1)] HOME = 1, [Global::P rotobuf.protoenum (name=@ "work", Value =2)] work = 2} Private global::P rotobuf.iextension extensionobject; Global::P rotobuf.iextension Global:: ProtoBuf.IExtensible.GetExtensionObject (bool createifmissing) { return global:: ProtoBuf.Extensible.GetExtensionObject (ref extensionobject, createifmissing);     }} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
3. Serialization and deserialization of the. cs file after proto conversion

First, copy the generated. cs file into your own project file
Then add the dynamic link library file Protobuf-net.dll (the file is located in the Protobuf-net_r668\protogen directory of the downloaded proto file)
Then refer to the program as follows:

Using System;Using System.Collections.Generic;Using System.Linq;Using System.Text;Using System.Threading.Tasks;Using Protobuf;Using Input.test;Using System.IO;Using System.Runtime.Serialization.Formatters.Binary;Namespacetest1{Classprogram {Staticvoid Main (String[] (args) {person p =new person (); p.name =  "Zhang San"; p.email = " [email protected] "; P.id = 12; //serialization operation MemoryStream Ms=new MemoryStream (); //binaryformatter BM = new BinaryFormatter (); //BM. Serialize (MS, p); Serializer.serialize<person> (MS, p); byte[] data = Ms. ToArray (); //length=27 709 //deserialization operation MemoryStream ms1 = new MemoryStream (data); //binaryformatter bm1 = new BinaryFormatter (); //person p1= BM. Deserialize (MS1) as person; person P1 = serializer.deserialize<person> (MS1); Console.readkey (); } }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

C # using Protobuf

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.