码迷,mamicode.com
首页 > Web开发 > 详细

protobuf-net序列化反序列化

时间:2016-04-20 13:25:57      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

下载:https://code.google.com/archive/p/protobuf-net/downloadsprotobuf-net 

解压引入protobuf-net.dll

namespace ProtobufProjectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Id = 90001;
            person.Name = "胡汉三";
            person.Addr = new Address { Line1 = "北大路", Line2 = "清华街" };

            // ProtoBuf序列化
            using (var file = System.IO.File.Create("Person"))
            {
                ProtoBuf.Serializer.Serialize(file, person);
            }

            // ProtoBuf反序列化
            Person binPerson = null;
            using (var file = System.IO.File.OpenRead("Person"))
            {
                binPerson = ProtoBuf.Serializer.Deserialize<Person>(file);
            }

            Console.WriteLine(binPerson.Id);
            Console.WriteLine(binPerson.Name);
            Console.WriteLine(binPerson.Addr.Line1);
            Console.WriteLine(binPerson.Addr.Line2);
        }
    }
}

[ProtoContract]
public class Address
{
    [ProtoMember(1)]
    public string Line1;
    [ProtoMember(2)]
    public string Line2;
}

[ProtoContract]
public class Person
{
    [ProtoMember(1)]
    public int Id;
    [ProtoMember(2)]
    public string Name;
    [ProtoMember(3)]
    public Address Addr;
}

 

protobuf-net序列化反序列化

标签:

原文地址:http://www.cnblogs.com/HelloUnity/p/5411968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!