我无法反序列化之前成功序列化的数据 [英] I cannot deserialize data previously serialized successfully
问题描述
我在 Google 中随机输入最快的序列化 c#",结果得到了 protobuf.net.我试过了,我觉得我还可以正确序列化,因为我不能反序列化,现在没有办法知道了吗?!
I was randomly typing 'fastest serializing c#' into Google and got protobuf.net as a result. I tried it and I think I can serialize properly yet since I can't deserilize, there is no way to tell now is there?!
当我试图反序列化时,我得到:
When trying to deserilaize I get :
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 等.
protobuf-net can be configured in many different ways. By default, it creates objects via a parameterless constructor, because that option that works on all frameworks. In this usage, it is a bit like XmlSerializer
. Because your types don't have a constructor, that usage can't work. The simplest option is to add a parameterless constructor. For use on the full framework this does not need to be public
- so a private
/ protected
etc constructor is fine - but note that this (private
/ protected
) won't work on Silverlight etc.
下一个选项是完全跳过构造函数 - 很像 DataContractSerializer
.这可以通过属性或通过类型模型的运行时配置来完成.为了说明第一个:
The next option is to skip the constructor completely - a lot like DataContractSerializer
. This can be done via attributes or via runtime configuration of the type-model. To illustrate the first:
[ProtoContract(SkipConstructor = true)]
public class Foo {...}
同样 - 这在大多数框架上都很好用,但有一些它不起作用(框架实用程序方法根本不存在).
Again - this works great on most frameworks, but there are a few where it doesn't work (the framework utility method to do it simply doesn't exist).
最后,您可以提供自己的工厂方法;每个类型的全局.这个工厂方法是一个 static
方法,它返回一个 vanilla 实例(可选地接受诸如序列化上下文、请求类型等).除了提供对构造的完全控制之外,如果您想提供对象池等,这也很有用.此选项适用于所有框架,但需要您编写额外的代码和配置.
Finally, you can provide your own factory methods; either per-type of globally. This factory method is a static
method that returns a vanilla instance (optionally accepting things like the serialization-context, requested-type, etc). In addition to providing complete control over construction, this is also useful if you want to provide object-pooling etc. This option works on all frameworks, but requires you to write extra code and configuration.
这篇关于我无法反序列化之前成功序列化的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!