class Program
{
static void Main(string[] args)
{
var library = new Library();
library.Keeper.Add("Harry Potter", "blablabla text... text...");
library.Keeper.Add("Other book", "Awesome story.");
// Write and read to test serializing.
Library deserialize;
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, library);
ms.Flush();
ms.Position = 0;
deserialize = Serializer.Deserialize<Library>(ms);
}
Console.WriteLine(deserialize.Keeper.Count);
}
}
[ProtoContract]
public class Library
{
public Dictionary<string, object> Keeper { get; set; }
[ProtoMember(1)]
public Dictionary<string, string> KeeperSer
{
get
{
var res = new Dictionary<string, string>();
foreach (var pair in Keeper)
{
res.Add(pair.Key, TypeDescriptor.GetConverter(pair.Value.GetType()).ConvertToInvariantString(pair.Value));
}
return res;
}
set
{
var set = new Dictionary<string, object>();
foreach (var pair in value)
{
set.Add(pair.Key, pair.Value);
}
Keeper = set;
}
}
public Library()
{
Keeper = new Dictionary<string, object>();
}
}
Keeper
KeeperSer
Keeper
MemoryStream
Library
Dictionary<string, object>
TypeDescriptor.GetConverter(Type)
KeeperSer
Through testing it seems that any IEnumerable<object>
compatible type just won't work in any way with Protobuf-net because it doesn't know how to handle it at all. The setter of those types never get called even though no error is thrown. Besides that, using a generic surrogate to 'cheat' the implicit operator
gives a recursive loop inside Protobuf-net.
For now, I think I need to go with just storing a textual representation of the object data and never use object
in conjunction with the Protobuf-net serializer.