Ron Idaho
Ron Idaho

Reputation: 67

Protobuf-net error serialising collection of enums values

I have a class:

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

(where DayOfWeek is the enum System.DayOfWeek).

I can serialise it with protobuf-net using this code:

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);
}

If I change Foo to this it also works:

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

If I change it to this:

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

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

It works if I mark up Foo with attributes, but I don't want to do that, so is there any way of getting this to serialise working directly with the model in code?

FURTHER INFO:

If I change Foo to:

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

And the serialising code to:

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

It still fails on Baz, even though it knows about DayOfWeek for Bar, which seems odd

FINALLY:

OK, so if I do this it works:

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

Which is OK, but I'm still interested in why it handles an enum value automatically in some circumstances, but not in others.

Upvotes: 1

Views: 82

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063629

This seems like a bug, so there isn't really anything to change in your code - it is a library problem; probably quite a simple one to resolve. It is probably best reported on GitHub.

Upvotes: 2

Related Questions