ProtoBuf-net 序列化 IEnumerable

发布于 2024-12-10 17:47:18 字数 1250 浏览 0 评论 0原文

我正在尝试在我的项目中使用 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.IEnumerable1[MyType]`。其次,我认为我无法根据手动和 protobuf-net 源代码分析对其进行反序列化。

  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 字段,对象值); } 使用私有字段。我已经使用这种方法来使用 Db4o 处理私有字段: http://community.versant .com/Forums/tabid/98/aft/10881/Default.aspx
  4. 除了创建继承之外,我还有哪些选项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.IEnumerable1[MyType]`. Second I think I will be unable to desirialize it based on manual, and protobuf-net source code analyzes.

  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> ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-12-17 17:47:18
  1. 目前没有,没有;公开的类型必须(至少)有一个Add方法。虽然我不反对调查在对象本身之外进行Add的可能性,但这很复杂,因为你正在查看一个不同的“主要”对象(序列与容器)。然而;如果您的 parent 对象实现了 IEnumerable (返回 _itemsSet.GetEnumerator() 等),那么它将找到 Add< /code> 自动

  2. 我看不到这里的上下文;但是,我怀疑如果没有Add,它仍然不愿意将其视为列表首先。不过,我明白你的处理方式,这也许是一种可以推理“我可以在此处使用 List”的方式,

  3. 老实说,这不是我调查过的东西;所以:不

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

乔纳森是对的,外部类的代理(拥有_itemsSetTSetAddT) 也可能是一个选项。

如果外部类存在以具有集合和add方法,则只需添加:IEnumerable并将AddT重命名为< code>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

  2. 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)

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

  4. 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 is right that a surrogate for the outer-class (the thing which has _itemsSet, TSet and AddT) 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 renaming AddT to Add would probably make it work.

分分钟 2024-12-17 17:47:18

我试图走捷径,并将我的模型重用为消息契约,但这实际上是错误的方法。正确的方法是创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文