Protobuf 继承和泛型
我正在尝试使用 ProtoBuf net 序列化具有以下格式的类的对象树:
[ProtoContract]
class MySpecialCollectionList<T> : List<MySpecialCollection<T>>
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
class MySpecialCollection<T> : List<Special<T>>
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
class Special<T>
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string Description { get; set; }
[ProtoMember(3)]
private readonly T _source;
T Source { get { return _source; } }
private Special()
{
}
public Special(T source)
{
_source = source;
}
}
interface IBeast
{
string Name { get; set; }
}
[ProtoContract]
class Ant : IBeast
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
class Cat : IBeast
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
class Dog : IBeast
{
[ProtoMember(1)]
public string Name { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MySpecialCollectionList<IBeast> collectionList = GetSpecialCollectionList();
using (var fs = File.Create(@"c:\temp\protobuftest.bin"))
{
Serializer.Serialize(fs, collectionList);
fs.Close();
}
}
private MySpecialCollectionList<IBeast> GetSpecialCollectionList()
{
var ant = new Ant() { Name = "Mr Ant" };
var cat = new Cat() { Name = "Mr Cat" };
var dog = new Dog() { Name = "Mr Dog" };
var Special = new Special<IBeast>(ant);
var specialCollection1 = new MySpecialCollection<IBeast>() {
{new Special<IBeast>(ant)},
{new Special<IBeast>(cat)},
{new Special<IBeast>(dog)}
};
specialCollection1.Name = "Special Collection1";
var specialCollection2 = new MySpecialCollection<IBeast>() {
{new Special<IBeast>(ant)},
{new Special<IBeast>(dog)}
};
specialCollection2.Name = "Special Collection2";
var specialCollectionList = new MySpecialCollectionList<IBeast>() {
specialCollection1, specialCollection2 };
specialCollectionList.Name = "Special Collection List";
return specialCollectionList;
}
}
请注意我正在序列化的类 (MySpecialCollectionList
) 是如何从 List
,而不仅仅是 List
。
我正在努力找出将“ProtoInclude”属性放在哪里,以使其序列化 MySpecialCollectionList 中的所有项目。任何帮助将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
继承在这里不是问题,因为即使
A : B
,Foo 也不是真的。 :Foo
。请注意,protobuf-net 不会使用非默认构造函数,尽管可以跳过构造函数,直接绑定到字段(甚至readonly
)。虽然您可能有 6T
,但我(从代码中)看不出您打算使用哪种封闭类型,并且如果已知封闭类型,则应该设置它。如果您有一个
Foo
和许多从SomeBaseClass
继承的具体类型,那么标记将在SomeBaseClass
上消失。但是,如果您有一个具体的场景我可以用来重现您的问题,我会很乐意看一下。
更新后的重新编辑:
示例中列出了几个关键点:
IList
的东西)本身有属性,它们将不会被序列化; 封装在这里优于继承,即有一个名称
和有一个列表(而不是有 >有一个名称
并且是一个列表)[ProtoMember]
移动到界面本身上,这是一个很好的 v2 版本:
Inheritance is not an issue here since even if
A : B
it is not true thatFoo<A> : Foo<B>
. Note that protobuf-net won't use a non-default constructor, although it is possible to skip the constructor, binding to the field directly (evenreadonly
). While you may have 6T
, I can't see (from the code) that it would ever be in doubt which closed type you intend, and if the closed type is known you should be set.If you have a
Foo<SomeBaseClass>
and a number of concrete types inherited fromSomeBaseClass
then the markers would o onSomeBaseClass
.However, if you have a concrete scenario I can use to reproduce your issue, I'll happily take a look.
Updated re edit:
There are a couple of key points drawn out in the example:
IList
) has properties itself, they will not be serialized; encapsulation is preferred over inheritance here, i.e. something that has aName
and has a list (rather than has aName
and is a list)[ProtoMember]
onto the interface itselfHere's a fully working version in v2: