我试图将真棒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现在可供下载 –