Protobuf-net:推荐的预编译Dictionary<string,CustomClass的方法>



>我目前正在使用以下代码来反序列化数据。

using (var zipData = new GZipStream(stream, CompressionMode.Decompress, true))
{
    data = Serializer.Deserialize<Dictionary<string, CustomClass>>(zipData);
}
自定义

类内部有许多嵌套的自定义对象。我想预编译一下。推荐的方法是什么?

谢谢。

它应该正常工作,即使在预编译时也是如此 - 只要CustomClass具有合适的 [ProtoContract] etc 属性,以便precompile.exe工具知道该怎么做。您可以通过使用单个根对象来进一步帮助它:

[ProtoContract]
public class SomeWrapper {
    [ProtoMember(1)]
    public Dictionary<string, CustomClass> Items {get;set;}
}

但这不是必需的,并且碰巧列表/字典/等作为根对象的输出与以列表/字典/等作为第一个成员的包装对象的输出 100% 相同[ProtoMember(1)])。

要进行预编译,请使用谷歌代码下载中的precompile.exe

precompile YourAppYour.dll –o:YourSerializer.dll –t:YourSerializer

然后,应用程序级代码变为:

using (var zipData = new GZipStream(stream, CompressionMode.Decompress, true))
{
    var serializer = new YourSerializer();
    data = (SomeWrapper)serializer.Deserialize(
        zipData, null, typeof(SomeWrapper));
}

相关内容

最新更新