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

查看:40
本文介绍了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.IEnumerable 1 [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>()方法吗?为什么只是不尝试将此属性设置为T []或 List< T> 或任何可分配给 IEnumerable< T> 的集合?
  3. 有没有一种方法可以提供自定义的反射机制来处理Silverlight中的私有字段,例如:实现: public interface IReflectable {object GetValue(FieldInfo field);SetValue(FieldInfo字段,对象值);} 以处理私有字段.我使用这种方法来处理Db4o的私有字段: http://community.versant.com/Forums/tabid/98/aft/10881/Default.aspx
  4. 除了创建继承的 MyTypeCollection< T>之外,我还有哪些选择?:Collection< T> 吗?
  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 方法.尽管我不反对在对象本身外部进行调查的可能性,但是这很复杂,因为您正在查看另一个主要"对象(序列与容器).然而;如果您的父级对象实现了 IEnumerable< T> (返回 _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< T>

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< T> ),并公开一个 Add(T)方法.它不必是 Collection< T> / List< T> /等-简单:它必须(目前)具有 some 机制添加. IList< T> 将是一个务实的选择,但我认为这并不是您想要的.

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 TSet AddT )也可以选择.

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

如果仅存在外部类 具有集合和add方法,则只需添加:IEnumerable< T> 并重命名 AddT 添加到 Add 可能会使其正常工作.

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天全站免登陆