没有为类型定义序列化程序:System.Windows.Media.Media3D.Point3D [英] No serializer defined for type: System.Windows.Media.Media3D.Point3D
问题描述
我正在尝试使用protobuf net序列化一些数据.在序列化过程中,我遇到一个错误,即没有为Point3D类型定义序列化.我发现了一个类似这样的问题,但仍然无法实现和解决.链接如下:-未为以下类型定义序列化程序:System.Drawing.颜色
I am trying to serialize some data using protobuf net. During the serialization I am getting an error that No serialization defined for the type Point3D. I found one issue something like this but still unable to implement and resolve it. Link is as follow :- No serializer defined for type: System.Drawing.Color
[ProtoContract]
public class ReturnPanelData
{
[ProtoMember(1)]
public Point3D PlacedPoint3D { get; set; }
[ProtoMember(2)]
public double PlacementAngle { get; set; }
[ProtoMember(3)]
public string PanelName { get; set; }
}
[ProtoContract]
public class ReturnDataType
{
[ProtoMember(1)]
public List<ReturnPanelData> ReturnList { get; set; }
[ProtoMember(2)]
public double RemainderArea { get; set; }
[ProtoMember(3)]
public int Height { get; set; }
[ProtoMember(4)]
public int Width { get; set; }
[ProtoMember(5)]
public Point3D BasePoint3D { get; set; }
}
class Program
{
private static HashSet<ReturnDataType> _processedList = new HashSet<ReturnDataType>();
static void Main(string[] args)
{
using (var file = File.Create(@"D:\SavedPCInfo2.bin"))
{
Serializer.Serialize(file, _processedList);
}
Console.WriteLine("Done");
}
}
我是JSON序列化/反序列化的初学者.如何解决此问题?
I am a begineer in JSON serialization/deserialization. How to resolve this issue ?
如果无法使用Protobuf Net序列化Point3D,那么还有什么其他方法可以序列化/反序列化一个非常大的列表(约有300000项)?
If it is not possible to serialize Point3D with Protobuf Net, what are the other options to serialize/deserialize a very big list (having approx 300000 items) ?
推荐答案
首先, protobuf-net 不是 JSON 序列化程序.它从协议缓冲区" 进行序列化-Google用于他们的大部分数据通信.
First off, protobuf-net is not a JSON serializer. It serializes from and to "Protocol Buffers" - the binary serialization format used by Google for much of their data communications.
话虽如此,但有几种解决方案可以使用protobuf-net序列化类型,而该类型不能用ProtoContract
属性修饰:
That being said, there are several solutions to serialize a type, using protobuf-net, that cannot be decorated with ProtoContract
attributes:
- 当包含在其他类型中时,请使用代理或"shim"属性,如下所示此处所示的替代方法,或
- 在运行时教导
RuntimeTypeModel
关于所有可序列化字段& 此处部分中所述的类型的属性没有属性的序列化.
- When contained by some other type, use a proxy or "shim" property as is shown here, OR
- Use a surrogate as is shown here, OR
- In runtime teach the
RuntimeTypeModel
about all serializable fields & properties of the type as described here in the section Serializing without attributes.
对于选项2,由于
For option 2, Since a Point3D
is entirely defined by its X, Y and Z coordinates, it's very easy to introduce a serialization surrogate:
[ProtoContract]
struct Point3DSurrogate
{
public Point3DSurrogate(double x, double y, double z) : this()
{
this.X = x;
this.Y = y;
this.Z = z;
}
[ProtoMember(1)]
public double X { get; set; }
[ProtoMember(2)]
public double Y { get; set; }
[ProtoMember(3)]
public double Z { get; set; }
public static implicit operator Point3D(Point3DSurrogate surrogate)
{
return new Point3D(surrogate.X, surrogate.Y, surrogate.Z);
}
public static implicit operator Point3DSurrogate(Point3D point)
{
return new Point3DSurrogate(point.X, point.Y, point.Z);
}
}
然后在启动时仅在protobuf-net中注册一次,如下所示:
And then register it with protobuf-net just once on startup like so:
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Point3D), false).SetSurrogate(typeof(Point3DSurrogate));
或者,对于选项3,可以在启动时为Point3D
定义合同,如下所示:
Alternatively, for option 3, in startup you could define a contract for Point3D
like so:
ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Point3D), true);
ProtoBuf.Meta.RuntimeTypeModel.Default[typeof(Point3D)].Add(1, "X").Add(2, "Y").Add(3, "Z");
(在我看来,尽管需要更多代码,但代理更清晰;完全在运行时定义协议似乎太麻烦了.)
(In my opinion the surrogate is clearer despite requiring more code; defining the protocol entirely in runtime seems too fiddly.)
我不建议使用选项1,因为您需要将代理属性添加到所有使用Point3D
的类中.
I don't recommend option 1 since you would need to add proxy properties to all classes that use Point3D
.
这篇关于没有为类型定义序列化程序:System.Windows.Media.Media3D.Point3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!