Protobuf-net 错误序列化枚举值的集合

Protobuf-net error serialising collection of enums values

我有一个 class:

public class Foo
{
    public DayOfWeek Bar { get; set; }
}

(其中 DayOfWeek 是枚举 System.DayOfWeek)。

我可以使用以下代码使用 protobuf-net 将其序列化:

RuntimeTypeModel runtimeTypeModel = RuntimeTypeModel.Create();

MetaType metaType = runtimeTypeModel.Add(typeof(Foo), false);

ValueMember valueMember = metaType.AddField(1, "Bar");

TypeModel model = runtimeTypeModel.Compile();

using (MemoryStream ms = new MemoryStream())
{
    model.Serialize(ms, foo);
}

如果我将 Foo 更改为此它也有效:

public class Foo
{
    public IReadOnlyList<int> Bar { get; set; }
}

如果我改成这样:

public class Foo
{
    public IReadOnlyList<DayOfWeek> Bar { get; set; }
}

我得到:System.InvalidOperationException:'No serializer for type System.DayOfWeek is available for model CompiledModel_c9ca355e-e813-4c8e-afb2-686b213506bb'

如果我用属性标记 Foo,它会起作用,但我不想那样做,所以有什么方法可以让它直接与模型一起序列化在代码中?

更多信息:

如果我将 Foo 更改为:

public class Foo
{
    public DayOfWeek Bar { get; set; }
    public IReadOnlyList<DayOfWeek> Baz { get; set; }
}

序列化代码为:

metaType.AddField(1, "Bar");
metaType.AddField(2, "Baz");

它仍然在 Baz 上失败,即使它知道 Bar 的 DayOfWeek,这看起来很奇怪

最终:

好的,所以如果我这样做就可以了:

runtimeTypeModel.Add(typeof(DayOfWeek), false);

没关系,但我仍然想知道为什么它在某些情况下会自动处理枚举值,而在其他情况下则不会。

这似乎是一个错误,因此您的代码实际上没有任何需要更改的地方 - 这是一个库问题;可能很容易解决。最好在 GitHub.

上报告