.NETでProtobufferを使用したシーケンス化と逆シーケンス化
1795 ワード
テキストアドレス
公式ダウンロードアドレス
エンティティークラスのテスト:
シーケンス化と逆シーケンス化
公式ダウンロードアドレス
エンティティークラスのテスト:
using ProtoBuf;
[ProtoContract]
public class Student
{
[ProtoMember(1)]
public int StudentId{get;set;}
[ProtoMember(2)]
public string Name { get; set; }
[ProtoMember(3)]
public string ClassName { get; set; }
}
シーケンス化と逆シーケンス化
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ProtoBuf;
namespace CenterServer
{
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(" " + stu.Name + "
" + " :" + stu.StudentId + "
" +
" :" + stu.ClassName);
s.Close();
}
Console.Read();
}
}
}