tags:

views:

64

answers:

1

I was using a version of v2 of protobuf-net from a few weeks ago quite successfully. (I want to use V2 due to the speed of the pre-compiled serializer running on the Compact Framework.) Everything worked great until I tried to serialize an object with a property of type Dictionary. I received the following error:

{"No serializer defined for type: System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

It looked like there were recent changes made to improve Dictionary support, so I grabbed the latest code from today. After building the CF3.5 and FF3.5 dlls, I tried again. Now I get a different error "The model cannot be changed once frozen." If I remove the ProtoMember attribute from the Dictionary property all seems to work well.

Has anyone succcessfully used a Dictionary in v2 of protobuf-net? Is this still too early of a release to be using? My speed tests showed v2 being twice as fast, does that seem accurate (validating my desire to use the less stable v2 pre-alpha bits.)

A: 

That is of definite interest. Do you have a specific demo that I can use to investigate? (I'm the author)


The problem is simply a bug; during Compile() it should cascade the model the see what other types are needed. It doesn't do this at the moment (but will soon ;p). In regular .NET (with RuntimeTypeModel) this isn't a problem - it can add in the extra type on the fly. But on CF (or with any static-compiled dll) this is not possible.

Until Compile() is fixed, the cheat here is to tell it what else it needs. The easiest way is to touch the indexer for KeyValuePair<string,string>:

model.Add(typeof(Parent), true);
var discard = model[typeof(KeyValuePair<string, string>)];
model.Compile(...);

We don't need the value returned by this indexer (into discard) - we just need it to have executed to fill in some blanks before calling compile.

Marc Gravell
I think I can put together a basic example and get it to you, thank you!
Steve
Thank you for the fix as well as the additional understanding.
Steve