日期:2014-05-18  浏览次数:20895 次

Dataset反序列化被阉
C# code

    [Serializable]
    public partial class MyDataSet : DataSet, ISerializable
    {
        public MyDataSet()
        { }
        protected MyDataSet(SerializationInfo info, StreamingContext context)  
        {}
    }
        private void btn反序列化_Click(object sender, EventArgs e)
        {      
            byte[] buffer = null;
            IFormatter formater = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            MemoryStream fs = new MemoryStream();
            DataTable dt=dsSYS_TestM.Tables[0].Copy();
            MyDataSet md = new MyDataSet();
            md.Tables.Add(dt);
            formater.Serialize(ms, md);
            buffer = new byte[(int)ms.Length];
            ms.Position = 0;
            ms.Read(buffer, 0, (int)ms.Length);
            ms.Close();
            IFormatter bina = new BinaryFormatter();
            MemoryStream ms1 = new MemoryStream(buffer);
            MyDataSet d = (MyDataSet)bina.Deserialize(ms1);
            ms1.Close();
        }

如上代码,最后 d 为空,反序列化不成功,请大家帮忙~

------解决方案--------------------
请用ms.ToArray()来获取buffer,否则在Read方法调用前,先flush,让数据立即写入。
------解决方案--------------------
不是很明白……学习
------解决方案--------------------
不懂!希望有高手来帮你解决