I have a Dictionary and i want to serialize it to call a method. In this method i wiil recive a string and i need to deserialize it to use. I want the way to convert Dictionary to string and to convert string to Dictionary again. There is any way to do it without using files?
Finally the code i've used is:
private string Serialize(Dictionary<string, object> parameters)
{
using (MemoryStream memoryStream = new MemoryStream()) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, parameters);
return Convert.ToBase64String(memoryStream.ToArray());
}
}
private Dictionary<string, object> Deserialize(string data)
{
using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data))) {
IFormatter formatter = new BinaryFormatter();
memoryStream.Seek(0, SeekOrigin.Begin);
return (Dictionary<string, object>)formatter.Deserialize(memoryStream);
}
}
Thanks!