更新时间:2023-02-14 11:41:19
小子,
确保通用列表中的类型可序列化.然后执行以下操作:
Hi Kid,
make sure the type in your generic list is serializable. Then do this:
List<yourtype> itemsToSerialize = new List<yourtype>();
itemsToSerialize.Add ( new yourtype() );
itemsToSerialize.Add ( new yourtype() );
Stream stream = new FileStream( @"MyApplicationData.dat", System.IO.FileMode.Create );
IFormatter formatter = new BinaryFormatter();
formatter.Serialize( stream, itemsToSerialize );
stream.Close();
</yourtype></yourtype>
本示例使用一个文件.但是您可以轻松地将其改写为写入套接字.这是将反序列化List< yourtype>的部分.来自文件:
This sample uses a file. But you can easyly adapt it to write to a socket instead. Here is the part that would deserialize List<yourtype> from a file:
Stream stream = new FileStream( @"MyApplicationData.dat", System.IO.FileMode.Open );
IFormatter formatter = new BinaryFormatter();
List<yourtyoe> itemsDeserialized = (List<yourtype>)formatter.Deserialize( stream );
stream.Close();
</yourtype></yourtyoe>
同样,您必须改用插座来代替它.
希望这会有所帮助!
欢呼声
Manfred
Again this will have to be adapted by you to utilize a socket instead.
Hope this helps!
Cheers
Manfred
听起来您想使用BinaryFormatter类来序列化和反序列化List<>放入可以发送到您的服务器的MemoryStream中: ^ ]
另外:您需要确保列表包含的对象类型是可序列化的(意味着它们已应用了[SerializableAttribute()]).
It sounds like you want to use the BinaryFormatter class to serialize and deserialize your List<> into a MemoryStream that can be sent to your server: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28VS.71%29.aspx[^]
Also: You need to be sure that the object type the list contains is serializable (meaning they have the [SerializableAttribute()] applied to them).