c# - Using ProtoBuf-net to deseriailize a derived type (dictionary) does not properly set object fields -


I am attempting to deserialize an object whose class meets the dictionary & lt; String, int & gt; with string member field

  Public class TempClass: dictionary & lt; String, int & gt; {Public string version; Public Tempelas () {}} I have written a unit test to capture the problem in which I am running: member field is not set, either serializing or deserializing ( / With a byte []) this test with the protofuf-net fails in the last  assurance  when it is verified that the deserialized  version  is set correctly it is always appropriate  is set to  null  instead of "someVersion" .   [testclass] Public class serial exam {[TestMethod] Public Zero TestMethod1 () {string newVersion = "someVersion"; TempClass Basic = New TempClass () {{"a", 2}, {"b", 3}, {"c", 1},}; Original. Version = new version; Byte [] serialized = serial (original); TempClass deserialized = desialize (sorted); // validate foreach (original pair originally) {Assert.IsTrue (deserialized.ContainsKeys (pair.key)); Assurance First (pair value, deserialized [couple.ca]); ) Doubt. All (new version, original version, "the original mapping version is not set correctly"); Decide. Aurequual (new version, deserialized.Version, "deserialized version does not match"); } Private static TempClass Deserialize (byte [serialized]) {TempClass deserialized; (MemoryStream MS = Using new memorystream ()) {ms.Write (sorted, 0, sorted. Length); Ms.osition = 0; Deserialized = Serializer Dairyialize & lt; TempClass & gt; (M / s); } Deserialized return; } Private static byte [] Serialize (Tempe class mapping) {byte [] serialized; (MemoryStream MS = new memorystream ()) using {Serializer.Serialize (MS, Mapping); Serialized = ms.ToArray (); } Return serialized; }}   

I have not tried the same work with BinaryFormatter and DataContractSerializer for any benefit < / P>

Follow-up question: Instead, I can redefine the TempClass in this way, always, the area is right from the original Instead of being set to version , the constructor is always called instead how can I deserialize and ring the constellation without creating a new version If only the original Make a copy?

  Public class TempClass: dictionary & lt; String, int & gt; {Public string version; Public TempClass () {Version = DateTime.UtcNow.ToString ("s"); } This effect may be related to the internal receipt of the ordering type of the IDV in the Protocol;}}    

. You can add version data to the dictionary or you can rewrite your DTO object like this sample. If you use this data object, it will correct your exam:

  [ProtoContract] Public class TempClass {[ProtoMember (1)] Public dictionary & lt; String, int & gt; Information; [Proto member (2)] public string version; Public Templas () {}}   

The third way is to write your own serialization.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -