I'm trying to integrate the awesome protobuf-net into an existing codebase, but am encountering a crash when it tries to handle a custom type. A small demonstration is below: it will throw an InvalidOperationException
in ProtoBuf.Serializers.ListDecorator
. But if you comment out the indexer (or remove the IEnumerable implementation) then it runs cleanly.
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>(); }
}
Am I doing something wrong? How can I tell the protobuf-net Serializer to just ignore that Indexer property?
Thanks!
EDIT (Oct 10): Marc has kindly provided a fix in protobuf-net r447 via [ProtoContract(IgnoreListHandling = true)]
.