没有为类型定义序列化器:System.Windows.Media.Media3D.Point3D

No serializer defined for type: System.Windows.Media.Media3D.Point3D

我正在尝试使用 protobuf net 序列化一些数据。在序列化过程中,我收到一个错误,指出没有为 Point3D 类型定义序列化。我发现了一个类似这样的问题,但仍然无法实施和解决。 Link 如下:- 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 serialization/deserialization 的初学者。如何解决这个问题?

如果无法使用 Protobuf Net 序列化 Point3D,serialize/deserialize 一个非常大的列表(大约有 300000 个项目)还有哪些其他选项?

首先,protobuf-net is not a JSON serializer. It serializes from and to "Protocol Buffers" - Google 用于大部分数据通信的二进制序列化格式。

话虽如此,有几种解决方案可以使用 protobuf-net 序列化类型,但不能用 ProtoContract 属性修饰:

  1. 当被其他类型包含时,使用代理或 "shim" 属性 如图所示 here,或
  2. 如图所示使用代理 here,或者
  3. 在运行时在无属性序列化.
  4. 部分教导RuntimeTypeModel about all serializable fields & properties of the type as described here

对于选项 2,由于 Point3D 完全由其 X、Y 和 Z 坐标定义,因此很容易引入序列化代理项:

[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 注册一次,如下所示:

ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Point3D), false).SetSurrogate(typeof(Point3DSurrogate));

或者,对于选项 3,您可以在启动时为 Point3D 定义一个合同,如下所示:

ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Point3D), true);
ProtoBuf.Meta.RuntimeTypeModel.Default[typeof(Point3D)].Add(1, "X").Add(2, "Y").Add(3, "Z");

(在我看来,尽管需要更多代码,但代理项更清晰;完全在运行时定义协议似乎过于繁琐。)

我不推荐选项 1,因为您需要向所有使用 Point3D.

的 类 添加代理属性