首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

MemoryStream 比较奇异! 见代码

2011-12-23 
MemoryStream 比较怪异! 见代码!MemoryStreammsnewMemoryStream()BinaryFormatterbfnewBinaryFormatter

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;

热点排行