Rumah > pembangunan bahagian belakang > Tutorial C#.Net > .NET中使用Protobuffer 实现序列化和反序列化的详解

.NET中使用Protobuffer 实现序列化和反序列化的详解

黄舟
Lepaskan: 2017-03-03 13:26:19
asal
2632 orang telah melayarinya

.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; }
    }
Salin selepas log masuk

然后对这个类进行序列化反序列化

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();
        }
   }
}
Salin selepas log masuk

现在考虑一下多个实体的情况,测试一下序列化一个集合

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();
        }
   }
Salin selepas log masuk

 以上就是.NET中使用Protobuffer 实现序列化和反序列化的详解的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan