gpt4 book ai didi

c# - Protobuf-net 和不可变消息的继承

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:46 31 4
gpt4 key购买 nike

我正在尝试序列化具有不可 rebase 类的不可变消息。但是,我无法说服 protobuf-serializer 将其反序列化为派生类型。简而言之,这个测试给了我一个无效的转换异常(从 Base 到 Derived):

[TestFixture]
public class InheritanceTest
{
public class Base
{
public int IntField { get; private set; }

public Base(int intField)
{
IntField = intField;
}
}

public class Derived : Base
{
public string StringField { get; private set; }

public Derived(int intField, string stringField) : base(intField)
{
StringField = stringField;
}
}

[Test]
public void TestInheritance()
{
var serializer = TypeModel.Create();
serializer.Add(typeof (Base), true)
.Add(1, "IntField")
.AddSubType(2, typeof (Derived))
.UseConstructor = false;

serializer.Add(typeof (Derived), true)
.Add(1, "StringField")
.UseConstructor = false;

serializer.CompileInPlace();

using (var stream = new MemoryStream())
{
var message = new Derived(1, "Some text that is not important");

serializer.Serialize(stream, message);
stream.Position = 0;

var retrieved = (Derived) serializer.Deserialize(stream, null, typeof (Derived));

Assert.AreEqual(message.IntField, retrieved.IntField);
Assert.AreEqual(message.StringField, retrieved.StringField);
}
}

}

如果我将 Base 和 Derived 转换为可变类型,此异常就会消失。我做错了什么还是 protobuf-net 的限制?

最佳答案

  [TestFixture]
public class InheritanceTest
{
public class Base
{
public int IntField { get; private set; }

public Base(int intField)
{
IntField = intField;
}
}

public class Derived : Base
{
public string StringField { get; private set; }

public Derived(int intField, string stringField) : base(intField)
{
StringField = stringField;
}
}

[Test]
public void TestInheritance()
{
var serializer = TypeModel.Create();
serializer.Add(typeof (Base), true)
.Add(1, "IntField")
.UseConstructor = false;

serializer.Add(typeof (Derived), true)
.Add(1, "StringField")
.UseConstructor = false;

serializer.CompileInPlace();

using (var stream = new MemoryStream())
{
var message = new Derived(1, "Some text that is not important");

serializer.Serialize(stream, message);
stream.Position = 0;

var retrieved = (Derived) serializer.Deserialize(stream, null, typeof (Derived));

Assert.AreEqual(message.IntField, retrieved.IntField);
Assert.AreEqual(message.StringField, retrieved.StringField);
}
}

}

  [TestFixture]
public class InheritanceTest
{
[DataContract]
public class Base
{
[DataMember(Order = 1)]
public int IntField { get; private set; }

public Base(int intField)
{
IntField = intField;
}
protected Base(){}
}

[DataContract]
public class Derived : Base
{
[DataMember(Order = 3)]
public string StringField { get; private set; }

public Derived(int intField, string stringField) : base(intField)
{
StringField = stringField;
}
private Derived(){}
}

[Test]
public void TestInheritance()
{
RuntimeTypeModel.Default.Add(typeof(Base), true).AddSubType(2, typeof(Derived));

using (var stream = new MemoryStream())
{
var message = new Derived(1, "Some text that is not important");

Serializer.Serialize(stream, message);
stream.Position = 0;

var retrieved = Serializer.Deserialize<Derived>(stream);

Assert.AreEqual(message.IntField, retrieved.IntField);
Assert.AreEqual(message.StringField, retrieved.StringField);
}
}

}

关于c# - Protobuf-net 和不可变消息的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615708/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com