ProtoBuf-net 序列化 IEnumerable<T> [英] ProtoBuf-net serializing IEnumerable<T>

查看:16
本文介绍了ProtoBuf-net 序列化 IEnumerable<T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目(主要是 Silverlight 4 项目)中使用 ProtoBuf-NET.

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

更新:首先我无法序列化它 - 没有为类型定义序列化器:System.Collections.Generic.IEnumerable1[MyType]`.其次,我想我将无法根据手动和 protobuf-net 源代码分析对其进行反序列化.

Update: First I can't serialize it - No serializer defined for type: System.Collections.Generic.IEnumerable1[MyType]`. Second I think I will be unable to desirialize it based on manual, and protobuf-net source code analyzes.

  1. 有没有办法扩展 protobuf-net 来为 ProtoMemeber 属性中的外部 Add 方法提供委托?
  2. 为什么使用 ProtoMember(1, OverwriteList=true) 不起作用?它不应该覆盖集合并且不应该关心 Add() 方法吗?为什么它只是不尝试将此属性设置为 T[] 或 List 或任何可分配给 IEnumerable 的集合?
  3. 有没有办法提供自定义反射机制来处理 Silverlight 中的私有字段,例如:实现:public interface IReflectable{ object GetValue(FieldInfo field);void SetValue(FieldInfo field, object value);} 使用私有字段.我已经使用这种方法来处理 Db4o 的私有字段:http://community.versant.com/Forums/tabid/98/aft/10881/Default.aspx
  4. 除了创建继承的MyTypeCollection之外,我还有什么选择?: 集合 ?
  1. Is there a way to extend protobuf-net to supply delegate to external Add method in ProtoMemeber attribute?
  2. Why using ProtoMember(1, OverwriteList=true) doesn't work? Doesn't it suppose to overwrite collection and shouldn't care about Add<T>() method? Why it just don't try to set this property to T[] or List<T> or any set assignable to IEnumerable<T> ?
  3. 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
  4. What options I have except creating inherited MyTypeCollection<T> : Collection<T> ?

推荐答案

  1. 目前没有,没有;公开的类型必须(至少)有一个 Add 方法.虽然我不反对调查在对象本身之外Add的可能性,但这很复杂,因为您正在查看不同的主要"对象(序列与容器).然而;如果您的 parent 对象实现了 IEnumerable(返回 _itemsSet.GetEnumerator() 等),那么它会找到 Add 自动

  1. 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 an Add 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 implemented IEnumerable<T> (returning _itemsSet.GetEnumerator() etc), then it would find the Add automatically

我看不到这里的上下文;但是,我怀疑如果没有 Add,它仍然不乐意将它放在首位.不过,我明白你的做法,这也许是一种可以推理的方式我可以在这里使用 List)

I 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 a List<T> here)

老实说,这不是我调查过的事情;所以:没有

It isn't something I've investigated, to be honest; so: no

属性中公开的类型至少必须:实现 IEnumerable(尽管 IEnumerable 是首选),并公开一个 Add(T) 方法.它不必是 Collection/List/等等 - 简单地说:它(目前)必须有 some添加的机制.IList 将是一个务实的选择,但我认为这不是您想要的.

The type exposed in the property must, as a minimum: implement IEnumerable (although IEnumerable<T> would be preferred), and expose an Add(T) method. It doesn't have to be Collection<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 是正确的外部类的代理( _itemsSet, TSetAddT) 也可能是一个选项.

Jonathan is right that a surrogate for the outer-class (the thing which has _itemsSet, TSet and AddT) might also be an option.

如果外部类only存在集合和add方法,那么只需添加:IEnumerable并重命名AddTAdd 可能会使它工作.

If the outer-class only exists to have the collection and the add method, then just adding : IEnumerable<T> and renaming AddT to Add would probably make it work.

这篇关于ProtoBuf-net 序列化 IEnumerable&lt;T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆