急啊,请高手帮帮忙啊

解决方案 »

  1.   


    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 的
      

  2.   


    下面的嵌套的结构,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
      

  3.   

    楼上兄弟有点不对了哦,下面是我用的//序列化
    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));
            }
      

  4.   

    我晕CSDN怎么这样,回复也出错,居然出一半,直接报错还好些.下面重贴
            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));
            }