Protobuf-Net serializes an object inaccessible to the prototype

Suppose I have the following class:

public class Test {
  int x { get; set; }
  int y { get; set; }
  Vector3 coords { get; set; }
}

How can I serialize this object if canont use [ProtoContract]also the [ProtoMember(x)]attribute in the class Vector3that comes from the external assembly.

I read. How can I serialize a third-party type using protobuf-net or other serializers? but it is undefined (for example, I donโ€™t know if I can mix the TypeModel approach and attributes or how to add a member of type unknowd as a field for a known type member, if I prefer to use only the TypeModel approach, etc.), so I need a concrete example for my situation.

For example, I declare TypeModel as follows:

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");

serialization / deserialization:

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)

+4
1

, : , ProtoContractAttribute, ; , , , [ProtoContract], f12, . , , - :

enter image description here

, "Generate class for" ProtoContract (... ...) "- , ctrl + ., enter ( ). , :

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());
    }
}
+2

All Articles