c# - 我无法反序列化之前成功序列化的数据

标签 c# protobuf-net

我在 Google 中随机输入“最快的序列化 C#”,结果得到了 protobuf.net。我试过了,我想我可以正确序列化,因为我不能反序列化,现在没有办法告诉我有没有?!

当尝试反序列化时,我得到:

A first chance exception of type 'ProtoBuf.ProtoException' occurred in protobuf-net.dll

很酷。

要序列化的数据:

[ProtoContract]
public struct Cow
{
    [ProtoMember(1)]
    public float Weight{ get; private set; }
    [ProtoMember(2)]
    public bool[] HadCowlings{ get; private set; }

    public Cow(float weight, bool[] babies)
        : this()
    {
        this.Weight = weight;
        this.HadCowlings= (bool[])babies.Clone();
    }
...
}

[ProtoContract]
public class Pasture
{
    [ProtoMember(1)]
    public Point Position { get; private set; }
    [ProtoMember(2)]
    public Cow[] Cows { get; private set; }

    public static int HerdSize { get; private set; }

    public static float BoundWidth { get; private set;}
    public static float BoundHeight { get; private set; }

    public Pasture(Cow[] Cows, Point farmPosition)
    {
        this.Cows = (Cow[])Cows.Clone();
        Position = farmPosition;
    }
...
}

[ProtoContract]
public class Farm
{
    [ProtoMember(1)]
    public Point FarmIDCoordinates{ get; private set; }
    [ProtoMember(2)]
    public List<Pasture> Pastures{ get; private set; }

    public static float BoundWidth { get; private set; }
    public static float BoundHeight { get; private set; }

    public static int FarmSize { get; private set; }

    public Farm(int x, int y, FarmType fType)
    {
        if (fType == RegionType.STANDARD)
            Pastures = new List<Pasture>(//make a farm!);
        else
            Pastures = new List<Pasture>(//What he said);

        FarmIDCoordinates = new Point(x, y);
    }
...
}

方法:

设置:

 using (ObjectSerializer serializer = new ObjectSerializer())
{
     serializer.ProtoSerialize<Farm>(farm.ToString() + ".bin", aFarm)
}

获取:

using (ObjectSerializer serializer = new ObjectSerializer())
{
   try
   {
      farmsIOwn.Add(serializer.ProtoDeserialize<Farm>(
                  farmLat.X.ToString() + "_" + farmLong.Y.ToString() + ".bin"));
   }
   catch
   {
      // make me a dummy farm, crashing is for dummies
   }
}

ObjectSerializer:

public void ProtoSerialize<T>(string fileName, T objectGraph)
{

   using (var stream = File.Open(fileName, FileMode.Create))
   {
       Serializer.Serialize<T>(stream, objectGraph);
   }
}

public T ProtoDeserialize<T>(string fileName)
{
    T objectGraph;

    using (var stream = File.Open(fileName, FileMode.Open))
    {
       objectGraph = Serializer.Deserialize<T>(stream);
    }

    return objectGraph;
}

最佳答案

protobuf-net 可以通过多种不同的方式进行配置。默认情况下,它通过无参数构造函数创建对象,因为该选项适用于所有框架。在这个用法上,有点像XmlSerializer。因为您的类型没有构造函数,所以该用法无效。最简单的选择是添加无参数构造函数。要在完整框架上使用,这不需要是 public - 所以 private/protected 等构造函数是好的 - 但请注意这个 ( private/protected) 不适用于 Silverlight 等。

下一个选项是完全跳过构造函数 - 很像 DataContractSerializer。这可以通过属性或通过类型模型的运行时配置来完成。为了说明第一个:

[ProtoContract(SkipConstructor = true)]
public class Foo {...}

同样 - 这在大多数框架上都很好用,但也有一些地方不起作用(框架实用方法根本不存在)。

最后,你可以提供自己的工厂方法;每种类型或全局。这个工厂方法是一个 static 方法,返回一个 vanilla 实例(可选地接受序列化上下文、请求类型等)。除了提供对构造的完全控制之外,如果您想提供对象池等,这也很有用。此选项适用于所有框架,但需要您编写额外的代码和配置。

关于c# - 我无法反序列化之前成功序列化的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139241/

相关文章:

c# - 我的统一着色器的问题

java - C# 中子字符串的实现及其使用的算法是什么?

c# - 将 MEF 与通用基类一起使用

c# - .Net 从哪个版本开始支持 httpclient

c# - 在 Protobuf-net 中序列化一个抽象列表

c# - 在 protobuf-net 中序列化 Type 类?

c# - 数据 GridView 中的多行列。使用 C#

c# - 通过网络传输 int

protobuf-net - 序列化为空

c# - 使用 protobuf-net 反序列化未知类型