Protobuf-Net NotSupportedException:类型不能表示为封闭不可变类型的默认值 (UnityEngine.Vector3) [英] Protobuf-Net NotSupportedException: Type cannot be represented as a default value for closed immutable type (UnityEngine.Vector3)

查看:28
本文介绍了Protobuf-Net NotSupportedException:类型不能表示为封闭不可变类型的默认值 (UnityEngine.Vector3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 如何使用 protobuf-net 序列化封闭的不可变类型Protobuf-net 和 Unity3D 类型,我尝试实现一个可以处理 UnityEngine 的 Vector3 容器的序列化器,其中唯一重要的值是 Vector3.x、Vector3.y 和 Vector3.z:

Following the answers to How to serialize a closed immutable type with protobuf-net and Protobuf-net and Unity3D types, I've tried implementing a serializer that can handle UnityEngine's Vector3 container, the only important values of which are Vector3.x, Vector3.y and Vector3.z:

使用以下类型模型:

serializer = TypeModel.Create();
serializer.UseImplicitZeroDefaults = false;

然后我分别尝试了两种不同的方法,为 Vector3 添加协议定义;一个明确的定义:

Then I tried two different methods, separately, of adding a protocol definition for Vector3; an explicit definition:

serializer.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");

并使用代理:

serializer.Add(typeof(Vector3), false).SetSurrogate(typeof(SurrogateVector3));

使用代理类:

[ProtoContract]
public sealed class SurrogateVector3
{
    [ProtoMember(1)]
    float x; 
    [ProtoMember(2)]
    float y; 
    [ProtoMember(3)]
    float z;

    public SurrogateVector3()
    {}

    public SurrogateVector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static implicit operator Vector3(SurrogateVector3 v)
    {
        return new Vector3(v.x, v.y, v.z);
    }

    public static implicit operator SurrogateVector3(Vector3 v)
    {
        return new SurrogateVector3(v.x, v.y, v.z);
    }
}

当使用任一方法尝试序列化 Dictionary 时,抛出以下异常:

When using either method to try and serialize a Dictionary<int, Vector3>, the following exception is thrown:

NotSupportedException: Type cannot be represented as a default value: UnityEngine.Vector3
ProtoBuf.Serializers.DefaultValueDecorator.EmitBranchIfDefaultValue (ProtoBuf.Compiler.CompilerContext ctx, ProtoBuf.Compiler.CodeLabel label) (at <5e93d5bf6f2048709aab19aea88deb74>:0)
...

我不确定如何修改我的 Typemodel 或协议定义以成功序列化 UnityEngine.Vector3 的集合.

I'm unsure of how to modify my Typemodel or protocol definitions in order to successfully serialize collections of UnityEngine.Vector3.

推荐答案

这可能是地图"代码中的一个错误,需要修复.您现在可以通过添加以下内容来避免它:

That's probably a bug in the "map" code, which needs fixing. You can probably avoid it for now by adding:

[ProtoMap(DisableMap = true)]

到作为字典的属性/字段.地图"代码和原始地图"代码之间的区别很微妙而且不是很有趣——它主要改变了重复情况下发生的事情——但是:地图"中似乎有一个恼人的错误逻辑,可能在原始代码路径中不存在.但是,地图"路径现在是默认路径,因此可以将其禁用.

to the property/field that is the dictionary. The difference between the "map" code and the original pre-"map" code is subtle and not very interesting - it mostly changes what happens in the case of duplicates - but: it would seem that there's an annoying bug in the "map" logic, that probably doesn't exist in the original code path. However, the "map" path is now the default, hence the workaround of disabling it.

这篇关于Protobuf-Net NotSupportedException:类型不能表示为封闭不可变类型的默认值 (UnityEngine.Vector3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆