2013-05-09 67 views
7

我正在使用Protobuf-net來序列化自定義嵌套列表。我知道原生列表不能直接嵌套,這就是爲什麼我使用容器對象作爲內部列表的原因。不過,我也想提出我的容器對象的IEnumerable但這意味着protobuf網把它扔了出去,出現錯誤:Protobuf-net:嵌套IEnumerable對象

Nested or jagged lists and arrays are not supported

這裏是我的表結構的一個例子這將導致錯誤:

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

修復方法是從MyInnerList中刪除IEnumerable,但很明顯這會阻止它直接迭代。有沒有像[ProtobufCustomObjectSoPleaseIgnoreIEnumerable]這樣可以使用的偷偷摸摸的屬性?

到目前爲止我想出的最佳選擇是使用Enumerable屬性,如下所示,但是我擔心該屬性仍然可以重新轉換回列表。我寧願以某種方式使用GetEnumerator/yield,但我看不出如何。

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

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

回答

8

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

燁:

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

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

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

此外,由於多個請求like this one,我打算看不久實施的交錯數組/列表的支持。該計劃基本上讓運行時在序列化程序的想象中用成員(字段1)欺騙封裝器,因此您可以使用List<List<T>>,它將像上面的模型一樣工作(它甚至可以是線路兼容的,因爲您明智地選擇了字段1)。

+0

非常完美,非常感謝。我不知道應該在哪裏尋找。 – 2013-05-09 10:22:36

+0

讓這個工作仍然有問題。此處發佈的問題:https://code.google.com/p/protobuf-net/issues/detail?id=287&q=IgnoreListHandling – 2013-05-09 16:14:23

+0

@shakinfree k;將看看 – 2013-05-09 17:20:51