How can a dictionary be sent to the client From Server ?
I want to do I send the following dictionary
Dictionary rates = new Dictionary<string,>();
rates.Add("usd", 47.5);
rates.Add("eur", 60.4);
rates.Add("ukp", 78.8);
Codes do not work.Where is my problem? Whether this approach is wrong ?
Please show me the correct code
code in server :
public void GetListn()
{
var dataToSend = new DataToSend { Rates = new Dictionary<string,>() };
dataToSend.Rates.Add("asd", decimal.Parse("12.23"));
dataToSend.Rates.Add("asd", decimal.Parse("41.11"));
dataToSend.Rates.Add("asd", decimal.Parse("25.23"));
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, dataToSend.Rates);
client.Send(stream.ToArray());
}
public class DataToSend
{
public Dictionary<string,> Rates { get; set; }
}
code in Client :
private void WindowLoaded(object sender, RoutedEventArgs e)
{
try
{
_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_server.Connect(_ipep);
_server.Send(Encoding.Unicode.GetBytes("GetList"));
_data = new byte[BufferSize];
_recv = _server.Receive(_data);
var stream = new MemoryStream(_server.Receive(_data));
var formatter = new BinaryFormatter();
var dataToSend = (DataToSend)formatter.Deserialize(stream);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
public class DataToSend
{
public Dictionary<string,> Rates { get; set; }
}