如何通过UDP连接C#发送泛型? [英] How to send generics over UDP connection C#?

查看:133
本文介绍了如何通过UDP连接C#发送泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以发送某种泛型,例如 List< float> floatValues = new List< float>()需要发送到udp客户端。我不知道该怎么做,将不胜感激!

I'm wondering is there a way to send some kind of generics for example List <float> floatValues = new List<float>() need to be sent to udp client. I don't know how to do that, any help will be appreciated!

推荐答案

您想要做的就是< a href = http://en.wikipedia.org/wiki/Serialization rel = nofollow>序列化/反序列化


在计算机科学中,在数据存储和传输的上下文中,序列化是将数据结构或对象状态转换为可以存储的格式(例如,在文件或内存缓冲区中,或在网络连接链接)并稍后在相同或其他计算机环境中恢复

In computer science, in the context of data storage and transmission, serialization, is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment

我建议您不要构建自己的序列化程序使用现有的库之一,例如
XmlSerializer
SoapFormatter
BinaryFormatter
DataContractSerializer
DataContractJsonSerializer
JavaScriptSerializer
Json.Net
ServiceStack
Protobuf.Net ........

Instead of building your own serializer, I would recommend to use one of the existing libraries like XmlSerializer, SoapFormatter, BinaryFormatter, DataContractSerializer , DataContractJsonSerializer, JavaScriptSerializer, Json.Net, ServiceStack, Protobuf.Net ........

以下是使用Json serializat的示例ion

Here is an example using Json serialization

//Sender
string jsonString = new JavaScriptSerializer().Serialize(floatValues);
byte[] bytesToSend = Encoding.UTF8.GetBytes(jsonString);

//Receiver
string receivedJson = Encoding.UTF8.GetString(bytesToSend);
List<float> floatValues2 = new JavaScriptSerializer()
                                         .Deserialize<List<float>>(receivedJson);

这篇关于如何通过UDP连接C#发送泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆