Exemplo n.º 1
0
    public void testProtoBuf()
    {
        // 测试 protoBuf;
        tutorial.Person person = new tutorial.Person();
        person.name  = "zyy";
        person.id    = 1234;
        person.email = "*****@*****.**";
        person.phones.Add(new tutorial.Person.PhoneNumber {
            number = "555-4321", type = tutorial.Person.PhoneType.HOME
        });
        person.phones.Add(new tutorial.Person.PhoneNumber {
            number = "222", type = tutorial.Person.PhoneType.WORK
        });

        // 写入文件;
        using (var output = File.Create("nToS.dat"))
        {
            //序列化对象到文件
            ProtoBuf.Serializer.Serialize <tutorial.Person>(output, person);
        }

        //// 从文件读出;
        //tutorial.Person per_2;
        //using (var input = File.OpenRead("nToS.dat"))
        //{
        //    //反序列化
        //    per_2 = ProtoBuf.Serializer.Deserialize<tutorial.Person>(input);
        //}
    }
Exemplo n.º 2
0
    public static void TestStream(PBStreamWriter s)
    {
        var p = new tutorial.Person();

        p.name = "hello";
        p.test.Add(1);
        p.test.Add(2);
        p.phone.Add(new tutorial.PhoneNumber
        {
            number = "789",
            type   = tutorial.PhoneType.WORK,
        });

        p.phone.Add(new tutorial.PhoneNumber
        {
            number = "456",

            type = tutorial.PhoneType.HOME,
        });

        var stream = new MemoryStream();

        ProtoBuf.Serializer.Serialize(stream, p);

        s.Stream.Position = 0;
        var data          = ProtoBuf.Serializer.Deserialize <tutorial.Person>(s.Stream);
        var compareStream = new MemoryStream();

        ProtoBuf.Serializer.Serialize(compareStream, data);


        if (CompareMemoryStreams(stream, compareStream))
        {
            int a = 1;
        }
    }