假设我有以下课程:
public class Test {
int x { get; set; }
int y { get; set; }
Vector3 coords { get; set; }
}
如果我不能在来自外部程序集的[ProtoContract]
类上使用[ProtoMember(x)]
和Vector3
属性,我如何序列化此对象。
我已阅读How can I serialize a 3rd party type using protobuf-net or other serializers?但它含糊不清(例如我不知道我是否可以混合TypeModel和属性方法,或者如果我选择将如何将未知类型成员添加为已知类型成员的字段只使用TypeModel方法等),所以我需要一个具体的例子来说明我的情况。
例如,我声明TypeModel如下:
RuntimeTypeModel.Default.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");
RuntimeTypeModel.Default.Add(typeof(SerializableTestClass), false).Add(1, "_x").Add(2, "_y").Add(3, "_coords");
序列化/反序列化:
if (GUILayout.Button("Serialize")) {
SerializableTestClass testClass = new SerializableTestClass();
testClass.changeMembers();
RuntimeTypeModel.Default.Serialize(_serializedObject, testClass);
}
if (GUILayout.Button("Deserialize")) {
SerializableTestClass test = (SerializableTestClass) RuntimeTypeModel.Default.Deserialize(_serializedObject, null, typeof(SerializableTestClass));
Debug.Log("Deserialized object: " + test.ToString());
}
当我尝试序列化时,我收到一个错误:
InvalidOperationException: Duplicate field-number detected; 1 on: SerializableTestClass
更新============================
现在,我更改了代码,所以一切都如下所示: 可序列化的类:
[ProtoContract]
public class SerializableTestClass {
[ProtoMember(1)]
int _x { get; set; }
[ProtoMember(2)]
int _y { get; set; }
[ProtoMember(3)]
Vector3 _coords { get; set; }
public SerializableTestClass() {
Debug.Log("SerializableTestClass.ctor()");
_x = 10;
_y = 20;
_coords = Vector2.one * 2;
}
public void changeMembers() {
_x += -3;
_y += 134;
_coords *= 3;
}
public override string ToString() {
return _x.ToString() + " " + _y + " " + _coords;
}
}
型号:
_model = TypeModel.Create();
_model.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");
_model.Add(typeof(SerializableTestClass), true);
序列化/反序列化:
if (GUILayout.Button("Serialize")) {
SerializableTestClass testClass = new SerializableTestClass();
_serializedObject = new MemoryStream();
testClass.changeMembers();
_model.Serialize(_serializedObject, testClass);
}
if (GUILayout.Button("Deserialize")) {
SerializableTestClass test = (SerializableTestClass) _model.Deserialize(_serializedObject, null, typeof(SerializableTestClass));
Debug.Log("Deserialized object: " + test.ToString());
}
输出:10 20 (2.0, 2.0, 2.0)
应该是:7 154 (6.0, 6.0, 6.0)
答案 0 :(得分:2)
这听起来很愚蠢,但以下是我意外再现的方法:检查你是否在本地定义了自己的ProtoContractAttribute
;基本上,检查将光标放在[ProtoContract]
上然后按 f12 会发生什么,或者右键单击并转到显示定义。您应看到的内容如下:
但是,在解析类型时,您可能会意外选择"为ProtoContract'生成类。 in(......各种选择......)" - 如果你当时没有实际参考,只需按 ctrl + 。,输入(添加使用的快速方法)。这会生成如下文件:
using System;
internal class ProtoContractAttribute : Attribute
{
}
这里重点是在错误的命名空间中,因此protobuf-net并不将其视为相关属性。
所以:如果你像我一样笨拙,这可能就是原因......
不相关,但如果构造函数中有非默认值,则应该在反序列化期间跳过构造函数;你这样做是通过:
[ProtoContract(SkipConstructor=true)]
以下是我使用常规.NET和伪造Vector3
的模型;它工作正常:
using ProtoBuf;
using ProtoBuf.Meta;
using System;
using System.IO;
[ProtoContract(SkipConstructor=true)]
public class SerializableTestClass
{
[ProtoMember(1)]
int _x { get; set; }
[ProtoMember(2)]
int _y { get; set; }
[ProtoMember(3)]
Vector3 _coords { get; set; }
public SerializableTestClass()
{
_x = 10;
_y = 20;
_coords = Vector3.one * 2;
}
public void changeMembers()
{
_x += -3;
_y += 134;
_coords *= 3;
}
public override string ToString()
{
return _x.ToString() + " " + _y + " " + _coords;
}
}
struct Vector3
{
public int x, y, z;
public static Vector3 one = new Vector3 { x = 1, y = 1, z = 1 };
public static Vector3 operator *(Vector3 value, int times)
{
return new Vector3
{
x = value.x * times,
y = value.y * times,
z = value.z * times
};
}
public override string ToString()
{
return string.Format("({0}, {1}, {2})", x, y, z);
}
}
class Program
{
static RuntimeTypeModel _model;
static void Main()
{
_model = TypeModel.Create();
_model.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z");
_model.Add(typeof(SerializableTestClass), true);
SerializableTestClass testClass = new SerializableTestClass();
var _serializedObject = new MemoryStream();
testClass.changeMembers();
Console.WriteLine("Original object: " + testClass.ToString());
_model.Serialize(_serializedObject, testClass);
_serializedObject.Position = 0;
Console.WriteLine(_serializedObject.Length);
SerializableTestClass test = (SerializableTestClass)_model.Deserialize(_serializedObject, null, typeof(SerializableTestClass));
Console.WriteLine("Deserialized object: " + test.ToString());
}
}