ProtoBuf-net 序列化 IEnumerable
我正在尝试在我的项目中使用 ProtoBuf-NET(主要是 Silverlight 4 项目)。
我在序列化我的模型集合时遇到困难,它们都是这样定义的:
private List<T> _itemsSet;
public IEnumerable<T> TSet
{
get {return _itemsSet;}
set {_itemsSet = value == null ? new List<T>() : new List<T>(value);}
}
public void AddT(T item)
{
//Do my logic here
_itemsSet.Add(item);
}
更新:首先我无法序列化它 - 没有为类型定义序列化程序:System.Collections.Generic.IEnumerable
1[MyType]`。其次,我认为我无法根据手动和 protobuf-net 源代码分析对其进行反序列化。
- 有没有办法扩展 protobuf-net 以向 ProtoMemeber 属性中的外部 Add 方法提供委托?
- 为什么使用
ProtoMember(1, OverwriteList=true)
不起作用?它不是应该覆盖集合并且不应该关心Add
方法吗?为什么它不尝试将此属性设置为 T[] 或() List
或任何可分配给IEnumerable
的集合? - 有没有办法提供自定义反射机制来处理 Silverlight 中的私有字段,例如: 实现:
public interface IReflectable{ object GetValue(FieldInfo field); void SetValue(FieldInfo 字段,对象值); }
使用私有字段。我已经使用这种方法来使用 Db4o 处理私有字段: http://community.versant .com/Forums/tabid/98/aft/10881/Default.aspx - 除了创建继承之外,我还有哪些选项
MyTypeCollection
?; : 集合
I'm trying to use ProtoBuf-NET in my project (it's mostly Silverlight 4 project).
I'm having difficulties serializing my Model collections, they all are defined like this:
private List<T> _itemsSet;
public IEnumerable<T> TSet
{
get {return _itemsSet;}
set {_itemsSet = value == null ? new List<T>() : new List<T>(value);}
}
public void AddT(T item)
{
//Do my logic here
_itemsSet.Add(item);
}
Update: First I can't serialize it - No serializer defined for type: System.Collections.Generic.IEnumerable
1[MyType]`. Second I think I will be unable to desirialize it based on manual, and protobuf-net source code analyzes.
- Is there a way to extend protobuf-net to supply delegate to external Add method in ProtoMemeber attribute?
- Why using
ProtoMember(1, OverwriteList=true)
doesn't work? Doesn't it suppose to overwrite collection and shouldn't care aboutAdd<T>()
method? Why it just don't try to set this property to T[] orList<T>
or any set assignable toIEnumerable<T>
? - Is there a way to supply custom reflection mechanism to work with private fields in Silverlight, like: implementing:
public interface IReflectable{ object GetValue(FieldInfo field); void SetValue(FieldInfo field, object value);
to work with private fields. I have used such approach to work with private fields with Db4o: http://community.versant.com/Forums/tabid/98/aft/10881/Default.aspx
} - What options I have except creating inherited
MyTypeCollection<T> : Collection<T>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前没有,没有;公开的类型必须(至少)有一个
Add
方法。虽然我不反对调查在对象本身之外进行Add
的可能性,但这很复杂,因为你正在查看一个不同的“主要”对象(序列与容器)。然而;如果您的 parent 对象实现了IEnumerable
(返回_itemsSet.GetEnumerator()
等),那么它将找到Add< /code> 自动
我看不到这里的上下文;但是,我怀疑如果没有
Add
,它仍然不愿意将其视为列表首先。不过,我明白你的处理方式,这也许是一种可以推理“我可以在此处使用List
”的方式,老实说,这不是我调查过的东西;所以:不
属性中公开的类型至少必须:实现
IEnumerable
(尽管IEnumerable
是首选),并公开一个Add(T)
方法。它不一定是Collection
/List
/ 等等 - 简单地说:它必须(目前)有一些机制来添加。IList
将是一个实用的选项,但我认为这并不完全是您想要的。乔纳森是对的,外部类的代理(拥有
_itemsSet
、TSet
和AddT
) 也可能是一个选项。如果外部类仅存在以具有集合和add方法,则只需添加
:IEnumerable
并将AddT
重命名为< code>Add 可能会使其工作。not at current, no; the type exposed must have (at a minimum) an
Add
method. While I have no objection to investigating the possibility of anAdd
outside the object itself, this is complicated since you are then looking at a different "primary" object (the sequence vs the container). However; if your parent object implementedIEnumerable<T>
(returning_itemsSet.GetEnumerator()
etc), then it would find theAdd
automaticallyI can't see the context here; however, I suspect that without the
Add
it still isn't happy to consider it a list in the first place. I see the way you are going with that, though, and it is perhaps a way it could reason "I can use aList<T>
here)It isn't something I've investigated, to be honest; so: no
The type exposed in the property must, as a minimum: implement
IEnumerable
(althoughIEnumerable<T>
would be preferred), and expose anAdd(T)
method. It doesn't have to beCollection<T>
/List<T>
/ etc - simply: it must (at present) have some mechanism to add.IList<T>
would be a a pragmatic option, but I gather that isn't quite what you want.Jonathan is right that a surrogate for the outer-class (the thing which has
_itemsSet
,TSet
andAddT
) might also be an option.If the outer-class only exists to have the collection and the add method, then just adding
: IEnumerable<T>
and renamingAddT
toAdd
would probably make it work.我试图走捷径,并将我的模型重用为消息契约,但这实际上是错误的方法。正确的方法是创建 DTO 的专用类和模型转换器,无论如何,将模型与消息分开是很好的。
不过,我必须批评 Marc,因为他添加了 ProtoContract 和 ProtoMemeber 属性,诱使用户通过归因来重用他们的模型。
I was trying to cut corners, and to reuse my model as a message contract, but this is actually wrong aproach. The way to go is to create DTO's specialized classes and converter to you model, anyway it's good to separate your model from messages.
I must criticize Marc though, for adding ProtoContract and ProtoMemeber attributes that lure users to reuse their model by attributing it.