【发布时间】:2011-10-04 21:10:21
【问题描述】:
我正在尝试将出色的 protobuf-net 集成到现有代码库中,但在尝试处理自定义类型时遇到了崩溃。下面是一个小演示:它将在ProtoBuf.Serializers.ListDecorator 中抛出一个InvalidOperationException。但是,如果您注释掉索引器(或删除 IEnumerable 实现),它就会运行干净。
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-net Serializer 忽略 Indexer 属性?
谢谢!
编辑(10 月 10 日):Marc 通过[ProtoContract(IgnoreListHandling = true)] 在protobuf-net r447 中提供了修复。
【问题讨论】:
-
顺便说一句;
[ProtoContract(IgnoreListHandling = true)]会这样做; r447 现已可供下载
标签: protobuf-net indexed-properties