protobuf网:嵌套的IEnumerable对象 [英] Protobuf-net : Nested IEnumerable objects

查看:180
本文介绍了protobuf网:嵌套的IEnumerable对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用protobuf网序列化的自定义嵌套列表。据我所知,本地列表不能直接嵌套,这就是为什么我使用了一个容器对象的内部列表。不过,我也想提出我的容器对象的IEnumerable但这意味着protobuf网抛出它的错误:




嵌套或参差不齐列表和数组不支持




下面是导致该错误我的表结构的一个例子:

  [ProtoContract] 
公共类MyOuterList< T>
{
[ProtoMember(1)]
只读表< MyInnerList< T>> nestedData =新的List<&链表类LT; T>>();
}

[ProtoContract]
公共类MyInnerList< T> :IEnumerable的< T>
{
[ProtoMember(1)]
私人只读表< T>数据=新的List< T>();
}



解决方法是从 MyInnerList 但显然,防止它被直接迭代。有没有像偷偷摸摸属性[ProtobufCustomObjectSoPleaseIgnoreIEnumerable] ,可以使用?



我想出了迄今为止最好的方法是使用一个可枚举属性,如下图所示,但我担心的财产仍可再转换回列表。我宁愿使用的GetEnumerator /产量以某种方式,但我看不出。

  [ProtoContract] 
公共类MyInnerList< T>
{
[ProtoMember(1)]
私人只读表< T>数据=新的List< T>();

公开的IEnumerable< T>数据
{
{返回this.data; }
}
}


解决方案

有没有像 [ProtobufCustomObjectSoPleaseIgnoreIEnumerable] 可用于?



偷偷摸摸属性

烨:

  [ProtoContract(IgnoreListHandling =真)] 
公共类MyInnerList< T> ; :IEnumerable的< T>
{
[ProtoMember(1)]
私人只读表< T>数据=新的List< T>();
}



鬼鬼祟祟偷偷摸摸的是。 IgnoreListHandling 的智能感知文档:




如果指定,不把这种类型列表,即使它看起来像有一个




此外,由于多个请求像这样的,我打算看短期内实施交错数组/列表的支持。该计划是基本上得到运行时欺骗与串行的想象成员(场1)包装,所以你可以使用列表<名单< T>> 和它会工作,就像你的模型之上(它甚至会成为线兼容的,既然你选择了理智领域 1 )。


I am using Protobuf-net to serialize a custom nested list. I understand that native lists cannot be nested directly, which is why I have used a container object for the inner list. However, I would also like to make my container objects IEnumerable but this means Protobuf-net throws it out with the error:

Nested or jagged lists and arrays are not supported

Here is an example of my list structure which causes the error:

[ProtoContract]
public class MyOuterList<T>
{
    [ProtoMember(1)]
    readonly List<MyInnerList<T>> nestedData = new List<ObjectList<T>>();
}

[ProtoContract]
public class MyInnerList<T> : IEnumerable<T>
{
    [ProtoMember(1)]
    private readonly List<T> data = new List<T>();
}

The fix is to remove IEnumerable from MyInnerList but obviously that prevents it being directly iterable. Is there a sneaky attribute like [ProtobufCustomObjectSoPleaseIgnoreIEnumerable] that could be used?

The best alternative I have come up with so far is to use an Enumerable property as shown below but I fear that the property could still be cast back to a list again. I would prefer to be using GetEnumerator/yield in some way but I can't see how.

[ProtoContract]
public class MyInnerList<T>
{
    [ProtoMember(1)]
    private readonly List<T> data = new List<T>();

    public IEnumerable<T> Data
    {
        get { return this.data; }
    }
}

解决方案

Is there a sneaky attribute like [ProtobufCustomObjectSoPleaseIgnoreIEnumerable] that could be used?

yup:

[ProtoContract(IgnoreListHandling=true)]
public class MyInnerList<T> : IEnumerable<T>
{
    [ProtoMember(1)]
    private readonly List<T> data = new List<T>();
}

sneaky is sneaky. IgnoreListHandling has the intellisense documentation:

If specified, do NOT treat this type as a list, even if it looks like one.

Also, due to multiple requests like this one, I plan on looking at implementing support for jagged arrays / lists shortly. The plan is to basically get the runtime to spoof the wrapper with a member (field 1) in the serializer's imagination, so you can use List<List<T>> and it'll work just like your model above (it will even be wire-compatible, since you sensibly chose field 1).

这篇关于protobuf网:嵌套的IEnumerable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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