I have just started working with ProtoBuf-Net and have the following objects:
[DataContract]
public class Statistics
{
[DataMember(Order = 1)]
public DateTime DateTimeAsUtc { get; set; }
[DataMember(Order = 2)]
public IEnumerable<StatisticsRow> TopHashTags { get; set; }
[DataMember(Order = 3)]
public IEnumerable<StatisticsRow> TopWords { get; set; }
}
[DataContract]
public class StatisticsRow
{
[DataMember(Order = 1)]
public string Key { get; set; }
[DataMember(Order = 2)]
public int Count { get; set; }
}
// Serialize then DeSerialize
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, stats);
stream.Seek(0, SeekOrigin.Begin);
var deserialized = Serializer.Deserialize<Statistics>(stream);
}
When I serialize and try deserializing the object I get the default value for DateTimeAsUtc
and null
for all the other properties. Any ideas on what I am doing wrong?
Note that I have tried replacing DataContract
and DataMember
with ProtoContract
and ProtoMember
to no avail.
The issue only happens when in Release
mode.
[Update]
The issue turned out to be due to existence of [MyConsoleApp].vshost.exe
which apparently is a special version of the executable to aid debugging which I thought would be re-generated after a Rebuild
(clearly not) so the solution was to manually delete it and now everything works just fine :-)
I am going to accept Marc's answer anyway as he was kind enough to follow it up and was very quick to reply.