I'm trying to serialize Dictionary<int, int>
as Dictionary<string, string>
.
So I've created Test2
type from Dictionary<int, int>
as follows:
[Serializable]
internal sealed class Test2 : Dictionary<int, int>
{
internal Test2()
{
}
private Test2(SerializationInfo info, StreamingContext context)
{
var data = (Dictionary<string, string>)
info.GetValue("data", typeof(Dictionary<string, string>));
foreach (var item in data)
Add(int.Parse(item.Key), int.Parse(item.Value));
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
var data = new Dictionary<string, string>();
foreach (var item in this)
data[item.Key.ToString()] = item.Value.ToString();
info.AddValue("data", data, typeof(Dictionary<string, string>));
}
}
and use the following code to test the serialization:
var test2 = new Test2 {{10, 10}};
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, test2);
stream.Position = 0;
var clone = (Test2) formatter.Deserialize(stream);
}
For some reason the clone does not contains any data (Count eq to 0).
Update:
"int" and "string" are here only for testing. In the real application I use something like Primary Key instead of string and big object instead of int and the serialized array contains relations between them. By cutting down the unrelated code and replacing the types I end up with with the example above.
I can only use .NET framework features.