如何通过UDP连接C#发送泛型

如何通过UDP连接C#发送泛型

本文介绍了如何通过UDP连接C#发送泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想知道是否有一种方法可以发送某种泛型,例如 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!推荐答案您想要做的就是序列化/反序列化 在计算机科学中,在数据存储和传输的上下文中,序列化是将数据结构或对象状态转换为可以存储的格式(例如,在文件或内存缓冲区中,或在网络连接链接)并稍后在相同或其他计算机环境中恢复 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 likeXmlSerializer,SoapFormatter,BinaryFormatter,DataContractSerializer ,DataContractJsonSerializer,JavaScriptSerializer,Json.Net,ServiceStack,Protobuf.Net ........以下是使用Json serializat的示例ionHere is an example using Json serialization//Senderstring jsonString = new JavaScriptSerializer().Serialize(floatValues);byte[] bytesToSend = Encoding.UTF8.GetBytes(jsonString);//Receiverstring receivedJson = Encoding.UTF8.GetString(bytesToSend);List<float> floatValues2 = new JavaScriptSerializer() .Deserialize<List<float>>(receivedJson); 这篇关于如何通过UDP连接C#发送泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 21:58