Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何保存列表<;点>;使用protobuf.net而不使用ascii文本?_List_Protobuf Net_Point - Fatal编程技术网

List 如何保存列表<;点>;使用protobuf.net而不使用ascii文本?

List 如何保存列表<;点>;使用protobuf.net而不使用ascii文本?,list,protobuf-net,point,List,Protobuf Net,Point,我正试图将列表中的点数据保存到protobuf.net创建的二进制文件中。虽然我自己没有问题,但我也在尝试以文本编辑器中不易查看的格式保存数据。默认情况下,将点结构列表保存到文本文件时,每个点的x和y都以ascii文本的形式显示 [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"BufPoints", DataFormat = global::ProtoBuf.DataFormat.Default)] pr

我正试图将列表中的点数据保存到protobuf.net创建的二进制文件中。虽然我自己没有问题,但我也在尝试以文本编辑器中不易查看的格式保存数据。默认情况下,将点结构列表保存到文本文件时,每个点的x和y都以ascii文本的形式显示

    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"BufPoints", DataFormat = global::ProtoBuf.DataFormat.Default)]
    private List<Point> BufPoints
    {
        get
        {
            return this.Points;
        }
        set
        {

            this.Points = value;

        }
    }

我不确定为什么深度克隆过程中会丢失这些值。有没有办法以另一种非ascii格式保存数据,或者有办法解决我的深层克隆问题?

首先,protobuf在任何时候都不是ascii(尽管字符串值存储为UTF-8,对于没有重音的拉丁字符,它通常看起来像ascii)-我无法在没有上下文的情况下评论您在那里看到的内容,但是序列化也不是加密。使用二进制格式可能会使人难以阅读/编辑内容,但不要将其用作安全检查的一部分

至于代码:我认为你把事情复杂化了。实际上,对于
System.Drawing.Point
System.Windows.Point
来说,作为“自动元组”处理的一部分,代码非常接近于自动计算映射。但不完全是这样!但我们可以简单地通过在应用程序启动时添加一行配置调整来教育它,告诉它通过将
.X
存储为字段1,将
.Y
存储为字段2来序列化
。这只是:

// either or both; whatever you need
model.Add(typeof(System.Windows.Point), false).Add("X", "Y");
model.Add(typeof(System.Drawing.Point), false).Add("X", "Y");
或者,如果您使用的是默认模型实例(即
序列化程序。*
方法):


而且。。。就这样!这就是你所需要的。
列表
点[]
的成员现在应该正确序列化和反序列化。

您能否澄清:您在什么时候将数据保存为ascii?Protobuf数据不是文本,不应在编辑器中读取/编辑。但是,序列化不是加密。
    [global::System.Serializable, global::ProtoBuf.ProtoContract(Name = @"PointConverter")]
class PointConverter
{
    [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name = @"X", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public double X;

    [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name = @"Y", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public double Y;

    public PointConverter(System.Windows.Point point)
    {
        this.X = point.X;
        this.Y = point.Y;
    }

    public PointConverter()
    {

    }

    public System.Windows.Point GetPoint()
    {
        return new System.Windows.Point(X, Y);
    }
}
// either or both; whatever you need
model.Add(typeof(System.Windows.Point), false).Add("X", "Y");
model.Add(typeof(System.Drawing.Point), false).Add("X", "Y");
// either or both; whatever you need
RuntimeTypeModel.Default.Add(typeof(System.Windows.Point), false).Add("X", "Y");
RuntimeTypeModel.Default.Add(typeof(System.Drawing.Point), false).Add("X", "Y");