protobuf-net可以序列化这种接口和泛型集合的组合吗?
我正在尝试序列化 ItemTransaction
并且 protobuf-net (r282) 有问题。
ItemTransaction : IEnumerable<KeyValuePair<Type, IItemCollection>></code>
而 ItemCollection 是这样的:
FooCollection : ItemCollection<Foo>
ItemCollection<T> : BindingList<T>, IItemCollection
IItemCollection : IList<Item>
其中 T 是 Item 的派生类型。 ItemCollection 还具有 IItemCollection 类型的属性。
我像这样进行序列化:
IItemCollection itemCol = someService.Blah(...);
...
SerializeWithLengthPrefix<IItemCollection>(stream, itemCol, PrefixStyle.Base128);
我的最终目标是序列化 ItemTransaction,但被 IItemCollection 所困扰。
Item 及其派生类型可以毫无问题地[反]序列化,请参阅[1],但反序列化 IItemCollection 会失败(序列化有效)。 ItemCollection 有一个 ItemExpression 属性,当反序列化 protobuf 时无法创建抽象类。这对我来说很有意义,但我不知道如何度过。
ItemExpression<T> : ItemExpression, IItemExpression
ItemExpression : Expression
ItemExpression 和 Expression 一样是抽象的
我如何让它正常工作?
另外,我担心 ItemTransaction 会失败,因为 IItemCollections 在编译时会有所不同且未知(ItemTransaction 将具有 FooCollection、BarCollection、FlimCollection、FlamCollection 等)。
我错过了什么(马克)?
I am trying to serialize a ItemTransaction
and protobuf-net (r282) is having a problem.
ItemTransaction : IEnumerable<KeyValuePair<Type, IItemCollection>></code>
and ItemCollection is like this:
FooCollection : ItemCollection<Foo>
ItemCollection<T> : BindingList<T>, IItemCollection
IItemCollection : IList<Item>
where T is a derived type of Item. ItemCollection also has a property of type IItemCollection.
I am serializing like this:
IItemCollection itemCol = someService.Blah(...);
...
SerializeWithLengthPrefix<IItemCollection>(stream, itemCol, PrefixStyle.Base128);
My eventual goal is to serialize ItemTransaction, but am snagged with IItemCollection.
Item and it's derived types can be [de]serialized with no issues, see [1], but deserializing an IItemCollection fails (serializing works). ItemCollection has a ItemExpression property and when deserializing protobuf can't create an abstract class. This makes sense to me, but I'm not sure how to get through it.
ItemExpression<T> : ItemExpression, IItemExpression
ItemExpression : Expression
ItemExpression is abstract as is Expression
How do I get this to work properly?
Also, I am concerned that ItemTransaction will fail since the IItemCollections are going to be differing and unknown at compile time (an ItemTransaction will have FooCollection, BarCollection, FlimCollection, FlamCollection, etc).
What am I missing (Marc) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对整个情况并不完全清楚;但是
Merge
可用于传递具体项目(如果您想自己创建一个空的具体实例并让 protobuf-net 填充属性)。如果
ItemExpression
使用[ProtoInclude(...)]
修饰以实现预期的ItemExpression
,则它应该 允许反序列化 - 只要它从未发现需要创建抽象类型,就支持抽象类型!另请参阅我的回答显示了它的使用情况。如果您可以提供一个我可以用来重现该问题的示例,我应该能够提供更多信息。
基于一些论坛外的例子,我想我已经得出结论,这个是受支持的,但是:
Deserialize...
,最外面的IList< ;T>
导数将默认创建为List
;您可以通过使用Merge
来解决此问题,传入您选择的具体列表实例来填充Item
、Foo
、< code>Bar 应标记为契约类型,并在Item
和Foo
以及Item
和之间使用适当的继承标记>Bar
但是是的;它应该有效。我已经通过电子邮件给您发送了一个示例,并打算整理上述最外层的方法。
I'm not entirely clear on the entire scenario; however
Merge
can be used to pass a concrete item in (in the case where you want to create an empty concrete instance yourself and let protobuf-net fill in the properties).If the
ItemExpression
is decorated with[ProtoInclude(...)]
for the expectedItemExpression<T>
it should allow deserialization - abstract types are supported just as long as it never finds it needs to create one! See also my answer here which shows this in use.If you can supply an example that I can use to reproduce the issue I should be able to provide more information.
Based on some off-forum examples, I think I've concluded that this is supported, but:
Deserialize...
, the outmostIList<T>
derivative will be created by default asList<T>
; you can get around this by usingMerge
instead, passing in a concrete list instance of your choosing to be filledItem
,Foo
,Bar
should be marked as contract-types, with appropriate inheritance markers betweenItem
andFoo
, andItem
andBar
But yes; it should work. I've e-mailed you a sample, and intend to tidy up the aforementioned outermost methods.