protobuf网不序列表< T> [英] protobuf-net do not serialize List<T>

查看:120
本文介绍了protobuf网不序列表< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化列表< T> 但得到空文件和列表< T> 不会连载。我没有得到任何异常,读protobuf网手动,这是我想要序列的所有成员都标有 [ProtoContract] [ProtoMember] atributes

 使用公共无效保存()
{
(VAR的OutputStream = File.Create(SettingsModel.QueueListDataFile))
{
Serializer.Serialize(OutputStream的,QueueList);
}
}

[Serializable接口]
[ProtoContract]
公共类QueueList:安全列表< QueueItem>
{

}

[Serializable接口]
[ProtoContract]
公共类安全列表< T> :SafeLock
{
[ProtoMember(1)]
私人静态只读表< T> ItemsList =新的List< T>();
}

[Serializable接口]
[ProtoContract]
公共类QueueItem
{
[ProtoMember(1)]
公字符串的SessionID {搞定;组; }
[ProtoMember(2)]
公共字符串电子邮件{获得;组; }
[ProtoMember(3)]
公共字符串叶{搞定;组; }
}


解决方案

protobuf网不看静态数据;你的主要数据是:



 私有静态只读表< T> ItemsList =新的List< T>(); 



据我所知,串行将着眼于这一点。序列化器是基于对象的;它们只在值上的对象的实例的兴趣。除此之外,还有继承的问题 - 你还没有定义它的模式,所以它被单独看每个



下面的工作正常,但坦白说,我怀疑这将是明智的,这里简化DTO模式;作为项目的列表一样简单的东西不应该包括3个级别的层次......其实,它不应该涉及的任何 - 列表< T> 工作的就好了






  ; 
使用系统;
使用System.Collections.Generic;
:使用System.IO;
静态类节目
{
使用公共静态无效的主要()
{
(VAR的OutputStream = File.Create(foo.bin))
{
VAR OBJ =新QueueList {};
obj.ItemsList.Add(新QueueItem {EMAIL =hi@world.com});
Serializer.Serialize(OutputStream中,OBJ);使用
}
(VAR的InputStream = File.OpenRead(foo.bin))
{
VAR OBJ = Serializer.Deserialize< QueueList>(InputStream的);
Console.WriteLine(obj.ItemsList.Count); // 1
Console.WriteLine(obj.ItemsList [0] .Email); // hi@world.com
}
}
}
[Serializable接口]
[ProtoContract]
公共类QueueList:安全列表< QueueItem>
{

}

[ProtoContract]
[ProtoInclude(1的typeof(安全列表< QueueItem>))]
公共类SafeLock {}

[Serializable接口]
[ProtoContract]
[ProtoInclude(2的typeof(QueueList))]
公共类安全列表< T> :SafeLock
{
[ProtoMember(1)]
公共只读目录< T> ItemsList =新的List< T>();
}

[Serializable接口]
[ProtoContract]
公共类QueueItem
{
[ProtoMember(1)]
公字符串的SessionID {搞定;组; }
[ProtoMember(2)]
公共字符串电子邮件{获得;组; }
[ProtoMember(3)]
公共字符串叶{搞定;组; }
}


I am trying to serialize List<T> but get empty file and List<T> won't serialize. I do not get any exception and read protobuf-net manual, all members which I want to serialize are marked with [ProtoContract] and [ProtoMember] atributes

public void Save()
{
    using (var outputStream = File.Create(SettingsModel.QueueListDataFile))
    {      
        Serializer.Serialize(outputStream, QueueList);
    }
}

[Serializable]
[ProtoContract]
public class QueueList : SafeList<QueueItem>
{

}

[Serializable]    
[ProtoContract]
public class SafeList<T> : SafeLock
{
    [ProtoMember(1)]
    private static readonly List<T> ItemsList = new List<T>();
}

[Serializable]
[ProtoContract]
public class QueueItem
{
    [ProtoMember(1)]
    public string SessionId { get; set; }
    [ProtoMember(2)]
    public string Email { get; set; }
    [ProtoMember(3)]
    public string Ip { get; set; }
}

解决方案

protobuf-net does not look at static data; your main data is:

private static readonly List<T> ItemsList = new List<T>();

AFAIK, no serializer will look at that. Serializers are object-based; they are only interested in the values on an instance of an object. Beyond that, there is an issue of inheritance - you have not defined it for the model, so it is looking at each separately.

The following works fine, but frankly I suspect it would be wise to simplify the DTO model here; something as simple as a list of items should not involve 3 levels of hierarchy... in fact, it shouldn't involve any - List<T> works just fine.


using ProtoBuf;
using System;
using System.Collections.Generic;
using System.IO;
static class Program
{
    public static void Main()
    {
        using (var outputStream = File.Create("foo.bin"))
        {
            var obj = new QueueList { };
            obj.ItemsList.Add(new QueueItem { Email = "hi@world.com" });
            Serializer.Serialize(outputStream, obj);
        }
        using (var inputStream = File.OpenRead("foo.bin"))
        {
            var obj = Serializer.Deserialize<QueueList>(inputStream);
            Console.WriteLine(obj.ItemsList.Count); // 1
            Console.WriteLine(obj.ItemsList[0].Email); // hi@world.com
        }
    }
}
[Serializable]
[ProtoContract]
public class QueueList : SafeList<QueueItem>
{

}

[ProtoContract]
[ProtoInclude(1, typeof(SafeList<QueueItem>))]
public class SafeLock {}

[Serializable]
[ProtoContract]
[ProtoInclude(2, typeof(QueueList))]
public class SafeList<T> : SafeLock
{
    [ProtoMember(1)]
    public readonly List<T> ItemsList = new List<T>();
}

[Serializable]
[ProtoContract]
public class QueueItem
{
    [ProtoMember(1)]
    public string SessionId { get; set; }
    [ProtoMember(2)]
    public string Email { get; set; }
    [ProtoMember(3)]
    public string Ip { get; set; }
}

这篇关于protobuf网不序列表&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆