My test code
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);
}
}
Library class
[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>();
}
}
I've also tried adding [ProtoIgnore] to the Keeper
property. Adding a breakpoint at the setter of either KeeperSer
or Keeper
never triggers when running the project. The getters do work and the data is being written by protobuf-net to the MemoryStream
. The length also varies when adding new items to the Library
.
The reason for the Dictionary<string, object>
is that I'm using TypeDescriptor.GetConverter(Type)
in another project. And I want to dynamically convert types to my required type.
What am I missing to make the setter of the proxy/shim property KeeperSer
working?