C# protobuf net不序列化列表<;T>;

C# protobuf net不序列化列表<;T>;,c#,protobuf-net,C#,Protobuf Net,我正在尝试序列化列表,但得到的文件为空,列表将不会序列化。我没有得到任何例外,并阅读protobuf网络手册,我想序列化的所有成员都标有[ProtoContract]和[ProtoMember]属性 public void Save() { using (var outputStream = File.Create(SettingsModel.QueueListDataFile)) { Serializer.Serialize(outputStream

我正在尝试序列化
列表
,但得到的文件为空,
列表
将不会序列化。我没有得到任何例外,并阅读protobuf网络手册,我想序列化的所有成员都标有
[ProtoContract]
[ProtoMember]
属性

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; }
}
public void Save()
{
使用(var outputStream=File.Create(SettingsModel.QueueListDataFile))
{      
序列化(outputStream,QueueList);
}
}
[可序列化]
[原始合同]
公共类队列列表:安全列表
{
}
[可序列化]
[原始合同]
公共类安全列表:安全锁
{
[原成员(1)]
私有静态只读列表项列表=新列表();
}
[可序列化]
[原始合同]
公共类队列项
{
[原成员(1)]
公共字符串SessionId{get;set;}
[原成员(2)]
公共字符串电子邮件{get;set;}
[原成员(3)]
公共字符串Ip{get;set;}
}

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

private static readonly List<T> ItemsList = new List<T>();
private static readonly List ItemsList=new List();
好了,没有序列化程序会看到这一点。序列化程序是基于对象的;他们只对对象实例上的值感兴趣。除此之外,还有一个继承问题——您还没有为模型定义它,所以它将分别研究每一个

下面的工作很好,但坦率地说,我怀疑在这里简化DTO模型是明智的;像项目列表这样简单的东西不应该包含3个层次结构。。。事实上,它不应该涉及任何-
列表
工作正常


使用ProtoBuf;
使用制度;
使用System.Collections.Generic;
使用System.IO;
静态类程序
{
公共静态void Main()
{
使用(var outputStream=File.Create(“foo.bin”))
{
var obj=新队列列表{};
obj.ItemsList.Add(新队列项目{Email=”hi@world.com" });
序列化(outputStream,obj);
}
使用(var inputStream=File.OpenRead(“foo.bin”))
{
var obj=序列化程序。反序列化(inputStream);
Console.WriteLine(obj.ItemsList.Count);//1
Console.WriteLine(obj.ItemsList[0].电子邮件);//hi@world.com
}
}
}
[可序列化]
[原始合同]
公共类队列列表:安全列表
{
}
[原始合同]
[协议包括(1,类型(安全列表))]
公共类安全锁{}
[可序列化]
[原始合同]
[ProtoInclude(2,typeof(QueueList))]
公共类安全列表:安全锁
{
[原成员(1)]
public readonly List ItemsList=new List();
}
[可序列化]
[原始合同]
公共类队列项
{
[原成员(1)]
公共字符串SessionId{get;set;}
[原成员(2)]
公共字符串电子邮件{get;set;}
[原成员(3)]
公共字符串Ip{get;set;}
}
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; }
}