Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BinaryFormatter Dictionary issue
using System;
using System.Linq;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
namespace BinaryFormatterDict
{
public class TestBinaryFormatter
{
public TestBinaryFormatter ()
{
}
#region DICT_BUFFER
static readonly byte [] dictReferenceBytes = new byte [] {
229,
84,
95,
75,
195,
48,
16,
79,
109,
215,
85,
196,
239,
208,
15,
80,
202,
166,
155,
115,
15,
125,
170,
162,
224,
203,
160,
178,
151,
82,
88,
90,
14,
13,
75,
19,
205,
31,
161,
126,
12,
63,
162,
136,
159,
66,
208,
220,
172,
224,
211,
222,
6,
202,
46,
228,
238,
114,
92,
142,
251,
221,
93,
66,
60,
66,
200,
167,
35,
148,
72,
1,
42,
175,
94,
209,
105,
3,
109,
154,
75,
206,
161,
49,
76,
10,
157,
94,
129,
0,
197,
154,
244,
130,
109,
12,
84,
117,
171,
147,
178,
236,
29,
11,
163,
152,
184,
75,
226,
86,
55,
82,
113,
86,
39,
241,
18,
148,
118,
110,
217,
36,
29,
225,
74,
226,
220,
114,
99,
21,
100,
2,
172,
81,
148,
39,
241,
194,
214,
156,
53,
55,
208,
221,
202,
53,
136,
172,
158,
205,
232,
180,
153,
158,
141,
231,
167,
19,
24,
157,
207,
171,
100,
151,
193,
171,
192,
225,
28,
246,
113,
162,
92,
182,
15,
84,
129,
138,
174,
169,
190,
47,
216,
51,
28,
187,
155,
75,
202,
45,
44,
40,
83,
154,
248,
196,
143,
94,
182,
21,
165,
151,
151,
143,
150,
114,
102,
186,
159,
120,
171,
241,
78,
43,
84,
69,
239,
219,
146,
250,
141,
225,
95,
247,
170,
172,
112,
42,
15,
15,
28,
243,
81,
65,
22,
224,
233,
47,
246,
4,
95,
209,
16,
51,
36,
94,
191,
253,
183,
189,
232,
82,
240,
225,
254,
145,
253,
128,
186,
153,
196,
53,
116,
131,
39,
132,
227,
121,
225,
160,
55,
132,
161,
83,
190,
173,
71,
95,
};
#endregion
public static void TestBinaryFormat ()
{
bool bufferOk = false;
bool dictOk = false;
BinaryFormatter fmt = new BinaryFormatter ();
var dict = new Dictionary<string, string> ();
dict.Add ("key", "value");
var dictSerializedStream = new MemoryStream ();
using (var gzip = new DeflateStream (dictSerializedStream, CompressionMode.Compress, true))
fmt.Serialize (gzip, dict);
dictSerializedStream.Seek (0, SeekOrigin.Begin);
var serializedBytes = dictSerializedStream.ToArray ();
bufferOk = dictReferenceBytes.SequenceEqual (serializedBytes);
using (MemoryStream ms = new MemoryStream (dictReferenceBytes)) {
using (var responseStream = new DeflateStream (new BufferedStream (ms), CompressionMode.Decompress)) {
var res = fmt.Deserialize (responseStream);
var dictOut = (System.Collections.Generic.Dictionary<string, string>)res;
dictOk = dictOut.OrderBy (pair => pair.Key).SequenceEqual (dict.OrderBy (pair => pair.Key));
}
}
Console.WriteLine (" Buffer OK: {0}, Dict OK: {0}", bufferOk, dictOk);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment