Protobuf-Net NotSupportedException:类型不能表示为封闭不可变类型的默认值(UnityEngine.Vector3)
Protobuf-Net NotSupportedException: Type cannot be represented as a default value for closed immutable type (UnityEngine.Vector3)
根据 How to serialize a closed immutable type with protobuf-net and Protobuf-net and Unity3D types 的答案,我尝试实现一个可以处理 UnityEngine 的 Vector3 容器的序列化程序,其唯一重要的值是 Vector3.x、Vector3.y 和 Vector3.z:
使用以下类型模型:
serializer = TypeModel.Create();
serializer.UseImplicitZeroDefaults = false;
然后我尝试了两种不同的方法,分别为 Vector3 添加协议定义;一个明确的定义:
serializer.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");
并使用代理人:
serializer.Add(typeof(Vector3), false).SetSurrogate(typeof(SurrogateVector3));
有代理人class:
[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<int, Vector3>
时,抛出以下异常:
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)
...
我不确定如何修改我的类型模型或协议定义才能成功序列化 UnityEngine.Vector3 的集合。
这可能是 "map" 代码中的错误,需要修复。您现在可以通过添加以下内容来避免它:
[ProtoMap(DisableMap = true)]
到property/field即字典。 "map" 代码和 "map" 之前的原始代码之间的区别是微妙的并且不是很有趣 - 它主要改变了重复情况下发生的事情 - 但是:似乎有一个烦人的错误"map" 逻辑,即 可能 在原始代码路径中不存在。但是,"map" 路径现在是默认路径,因此禁用它的解决方法。
根据 How to serialize a closed immutable type with protobuf-net and Protobuf-net and Unity3D types 的答案,我尝试实现一个可以处理 UnityEngine 的 Vector3 容器的序列化程序,其唯一重要的值是 Vector3.x、Vector3.y 和 Vector3.z:
使用以下类型模型:
serializer = TypeModel.Create();
serializer.UseImplicitZeroDefaults = false;
然后我尝试了两种不同的方法,分别为 Vector3 添加协议定义;一个明确的定义:
serializer.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");
并使用代理人:
serializer.Add(typeof(Vector3), false).SetSurrogate(typeof(SurrogateVector3));
有代理人class:
[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<int, Vector3>
时,抛出以下异常:
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)
...
我不确定如何修改我的类型模型或协议定义才能成功序列化 UnityEngine.Vector3 的集合。
这可能是 "map" 代码中的错误,需要修复。您现在可以通过添加以下内容来避免它:
[ProtoMap(DisableMap = true)]
到property/field即字典。 "map" 代码和 "map" 之前的原始代码之间的区别是微妙的并且不是很有趣 - 它主要改变了重复情况下发生的事情 - 但是:似乎有一个烦人的错误"map" 逻辑,即 可能 在原始代码路径中不存在。但是,"map" 路径现在是默认路径,因此禁用它的解决方法。