急啊,请高手帮帮忙啊
解决方案 »
- |ZYCWPF| 我现在用C#想实现一个RTMP的实时MP4直播流,不知道大家有没有什么好的办法,麻烦大家指点
- 怎样遍历使表中的数据生成树?
- c# 钩子问题
- DataGridView的事件
- 如何通过反射调用构造函数
- 字符串的问题
- 如何用C#来判断我的电脑上是否装瑞星了,如果装了,装在那个盘里.
- DataGridView选择数据问题
- 写 [DllImportAttribute("kernel32.dll")] public extern static int GetLogicalDrives(); ide怎么不能自动提示 GetLogicalDrives();
- .sfv文件不知用什么软件打开
- asp.net(c#)中如何动态添加dropdownlist控件
- 关于Winfrom窗体的问题
using(MemoryStream ms = new MemoryStream())
try
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dataSet); // 一个可Serializable对象 byte[] dataSetBytes = ms.ToArray(); // 转到字节里了
}using(MemoryStream ms = new MemoryStream(bdataSetBytes))
{
ms.Position = 0;
BinaryFormatter bf = new BinaryFormatter();
DataSet dataSet = (DataSet)bf.Deserialize(ms); // 反序列化为 DataSet对象
}
1)名称空间如下
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
2)类型必须是 Serializable 的
下面的嵌套的结构,C# 中, 定义的演示了此问题。 该结构是嵌套的三层。
[Serializable]
public struct TopLevel
{
public SecondLevel second ;
}
[Serializable]
public struct SecondLevel
{
public ThirdLevel third ;
}
[Serializable]
public struct ThirdLevel
{
public int i;
}
下面的 C# 示例代码段,使用 BinaryFormatter ,重现此问题由序列化和反序列化嵌套结构的实例:
// Initialize the nested structs.
TopLevel tl = new TopLevel();
tl.second = new SecondLevel ();
tl.second.third = new ThirdLevel ();
tl.second.third.i = 5;
添加上的 C# 文件的顶部这些行:
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
将结构到流,然后再试着反序列化结构:
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, tl);
stream.Position = 0;
Object obj = formatter.Deserialize(stream);
可以通过 .NET Remoting 使用 BinaryFormatter 。 by default,Transmission Control Protocol (TCP) channel uses BinaryFormatter . if pass same structs to remote object by using .NET Remoting with binary serialization,then will experience same problem.
http://support.microsoft.com/kb/320079/zh-cn
private byte[] SerializeList<T>(T list)
{
byte[] listByteTemp;
MemoryStream tempStream = new MemoryStream();
BinaryFormatter temp = new BinaryFormatter();
temp.Serialize(tempStream, list);
listByteTemp = tempStream.GetBuffer();
return listByteTemp;
}
//反序列化
private T DeserializeList<T>(byte[] listByteTemp)
{ LeafClassDataPack leafClassDataPack;
BinaryFormatter leafBinaryFormatterTemp = new BinaryFormatter();
return (T)leafBinaryFormatterTemp.Deserialize(new MemoryStream(listByteTemp));
}
private byte[] SerializeList<T>(T list)
{
byte[] listByteTemp;
MemoryStream tempStream = new MemoryStream();
BinaryFormatter temp = new BinaryFormatter();
temp.Serialize(tempStream, list);
listByteTemp = tempStream.GetBuffer();
return listByteTemp;
}
//反序列化
private T DeserializeList<T>(byte[] listByteTemp)
{ LeafClassDataPack leafClassDataPack;
BinaryFormatter leafBinaryFormatterTemp = new BinaryFormatter();
return (T)leafBinaryFormatterTemp.Deserialize(new MemoryStream(listByteTemp));
}