The following code works as-is on WP7 and Windows, I am now trying to get it to run on MonoDroid:
[ProtoContract]
public class SSDTO {
[ProtoMember(1)]
public Dictionary<开发者_JS百科string, string> Strings = new Dictionary<string, string>(50);
[ProtoMember(2)]
public Dictionary<string, int> Ints = new Dictionary<string, int>(50);
[ProtoMember(3)]
public Dictionary<string, byte[]> Bytes = new Dictionary<string, byte[]>(10);
}
public class SettingStore {
public event EventHandler ContentsChanged;
private Dictionary<string, string> _StringVals;
private Dictionary<string, int> _IntVals;
private Dictionary<string, byte[]> _ByteVals;
public SettingStore() {
_StringVals = new Dictionary<string, string>(50);
_IntVals = new Dictionary<string, int>(50);
_ByteVals = new Dictionary<string, byte[]>(10);
}
private SettingStore(SSDTO source) {
_StringVals = source.Strings;
_IntVals = source.Ints;
_ByteVals = source.Bytes;
}
//Accessors removed
public static SettingStore DeSerialize(Stream data) {
return new SettingStore(Serializer.Deserialize<SSDTO>(data));
}
public void Serialize(Stream Target) {
Serializer.Serialize<SSDTO>(Target, toDTO());
}
private SSDTO toDTO() {
return new SSDTO { Ints = this._IntVals, Strings = this._StringVals, Bytes = this._ByteVals };
}
}
The Exception I get:
System.InvalidOperationException: No serializer defined for type: System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] at ProtoBuf.Meta.ValueMember.BuildSerializer () [0x00000] in :0 at ProtoBuf.Meta.ValueMember.get_Serializer () [0x00000] in :0 at ProtoBuf.Meta.MetaType.BuildSerializer () [0x00000] in :0 at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in :0 at ProtoBuf.Meta.RuntimeTypeModel.Serialize (Int32 key, System.Object value, ProtoBuf.ProtoWriter dest) [0x00000] in :0 at ProtoBuf.Meta.TypeModel.SerializeCore (ProtoBuf.ProtoWriter writer, System.Object value) [0x00000] in :0 at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value, ProtoBuf.SerializationContext context) [0x00000] in :0 at ProtoBuf.Meta.TypeModel.Serialize (System.IO.Stream dest, System.Object value) [0x00000] in :0 at ProtoBuf.Serializer.Serialize[SSDTO] (System.IO.Stream destination, ABC.SystemModel.SSDTO instance) [0x00000] in :0 at ABC.SystemModel.SettingStore.Serialize (System.IO.Stream Target) [0x00002] in C:\CODE\SettingStore.cs:145
I compiled protobuf-net for Monodroid in release from the monodroid project in the source trunk, which I grabbed 3-4 days ago.
I had this reported to me a few days ago; it is an accidental regression, due to the Mono KeyValuePair having different accessors than the .NET one, and the new "tuple" handling not agreeing that it is a suitable match (basically, in Mono they have private setters, which counted against it).
I will fix this this weekend and redeploy. My sincere apologies.
精彩评论