110,534
社区成员
发帖
与我相关
我的任务
分享
基元类型(如:整数、字符串和字节数组)以及某些特殊类型(如 XmlElement 和 DateTime),这些特殊类型也被视为基元类型。
数据协定类型(用 DataContractAttribute 属性标记的类型)。
用 SerializableAttribute 属性标记的类型,包括实现 ISerializable 接口的类型。
实现 IXmlSerializable 接口的类型。
许多常见集合类型,包括许多泛型集合类型。
许多 .NET Framework 类型属于后两种类别,因此可序列化。可序列化类型的数组也可序列化。有关完整列表,请参见在服务协定中指定数据传输。
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999);
socket.Connect(ep);
HelloBox helloBox = new HelloBox();
helloBox.text = text;
helloBox.pic = pic;
XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, helloBox);
byte[] data = new byte[1024];
ms.Position = 0;
while (ms.Read(data, 0, 1024) > 0)
{
socket.Send(data);
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999);
socket.Bind(ep);
socket.Listen(100);
Socket client = socket.Accept();
XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms = new MemoryStream();
byte[] data = new byte[1024];
while (client.Receive(data) > 0)
{
ms.Write(data,0,1024);
}
ms.Position = 0;
HelloBox helloBox = xs.Deserialize(ms) as HelloBox;
return helloBox;
Image image = VerifyCode.NextImage(6);
//XmlSerializer s = new XmlSerializer(image.GetType());
BinaryFormatter s = new BinaryFormatter();
using(FileStream fs = new FileStream("1.bin", FileMode.Create))
{
s.Serialize(fs, image);
}
using(FileStream fs = new FileStream("1.bin", FileMode.Open))
{
image = s.Deserialize(fs) as Image;
}
image.Save("1.png", ImageFormat.Png);