我試圖將真棒protobuf-net集成到現有的代碼庫中,但在嘗試處理自定義類型時遇到崩潰。一個小示例如下:它將在ProtoBuf.Serializers.ListDecorator
中拋出InvalidOperationException
。但是,如果您註釋掉索引器(或刪除IEnumerable實現),那麼它會乾淨地運行。protobuf-net如何避免在使用索引屬性時崩潰
using System.Collections.Generic;
using ProtoBuf;
using System.Collections;
[ProtoContract]
public class MyClass : IEnumerable<int>
{
[ProtoMember(1, IsPacked = true)]
public int[] data { get; set; }
// Comment out this indexed property to prevent the crash
public int this[int i] { get { return data[i]; } set { data[i] = value; } }
public IEnumerator<int> GetEnumerator() { foreach (var x in data) yield return x; }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
static void Main(string[] args) { Serializer.PrepareSerializer<MyClass>(); }
}
我做錯了什麼?我怎麼能告訴protobuf網序列化器忽略該索引器屬性?
謝謝!
編輯(10月10日):Marc已經通過[ProtoContract(IgnoreListHandling = true)]
親切地提供了protobuf-net r447的修復。
順便說一句; '[ProtoContract(IgnoreListHandling = true)]'會做到這一點; r447現在可供下載 –