Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protobuf-net enum serialization

What needs to be done in order to serialize enums with protobuf-net? I'm getting the below error when serializing a class having an enum property, if the DataMember attribute is removed from the property declaration it works fine.

"System.InvalidOperationException: Only data-contract classes (and lists/arrays of such) can be processed"

like image 676
Kimi Avatar asked Nov 09 '10 15:11

Kimi


1 Answers

I suspect they are actually 2 different scenarios, but with regard to the code sample added by Andrew, this is because it can't figure out (in advance) what it is going to do with regards to default values (by default, data is treated as optional at the receiver). There are 3 ways of fixing this:

1: add an enum with value 0 (since 0 is always the CLI default value for zeros), for example

public enum SiteType
{
    Error = 0,
    ...

2: tell it which value to use by default:

[ProtoMember(10), DefaultValue(SiteType.Partition)]
public SiteType Type { get; set; }

3: tell the engine that it really doesn't need to worry about it, i.e. that it is going to have a value:

[ProtoMember(10, IsRequired = true)]
public SiteType Type { get; set; }
like image 183
Marc Gravell Avatar answered Sep 20 '22 12:09

Marc Gravell