日期:2014-05-19  浏览次数:21022 次

MemoryStream 比较怪异! 见代码!
MemoryStream   ms   =   new   MemoryStream();
BinaryFormatter   bf   =   new   BinaryFormatter();

for   (int   i   =   0;   i   <   dt.Rows.Count;   i++)
{
    bf.Serialize(ms,   dt.Rows[i][0]);
}
byte[]   byteBuffer   =   new   byte[ms.Length];
ms.Read(byteBuffer,   0,   (int)ms.Length);

为什么byteBuffer中的值都是0呢,郁闷,怪,不懂

------解决方案--------------------
#region SerializeToStream
public static Stream SerializeToStream(object obj)
{
Stream s = (Stream)(new MemoryStream());
SerializeToStream(s, obj);
return s;
}

public static void SerializeToStream(Stream s, object obj)
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s, obj);
}
#endregion
------解决方案--------------------
ms拿完数据后,还要读就得 ms.Position = 0;