序列化问题。

stning 2009-10-25 11:05:28
    [Serializable]
public class Message
{
public byte[] ToBytes()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new System.IO.MemoryStream();
formatter.Serialize(stream, this);
byte[] bytes = stream.GetBuffer();
stream.Close();
return bytes;
}

public static Message ReadBytes(byte[] bytes, int lenght)
{
MemoryStream stream = new MemoryStream(bytes, 0, lenght);
BinaryFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj as Message;
}
}


二进制流“0”不包含有效的 BinaryHeader。这可能是由于无效流,或由于在序列化和反序列化之间的对象版本更改。
不知道什么问题了,试了很多种。麻烦大家帮我看看。我序列化的是一个对象来的
...全文
134 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
stning 2009-10-26
  • 打赏
  • 举报
回复
up
深海之蓝 2009-10-26
  • 打赏
  • 举报
回复
可以把想序列化的东西放到类里边,用上边的就行了
深海之蓝 2009-10-26
  • 打赏
  • 举报
回复
Stream s = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(s, sc);
s.Close();

反序列化
Stream s = File.Open(filename,FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
object ob = bf.Deserialize(s);
你原来的类型(例如 A 类的对象 a) = ob as A;
qiao246 2009-10-26
  • 打赏
  • 举报
回复
拿分
lzhdim 2009-10-26
  • 打赏
  • 举报
回复
跟踪你序列化后的文本流。如果该文本流不对,那么你反序列化自然有问题。
lovelan1748 2009-10-25
  • 打赏
  • 举报
回复
哦,传进去的byte[]有值吗
lovelan1748 2009-10-25
  • 打赏
  • 举报
回复
报的什么错啊
stning 2009-10-25
  • 打赏
  • 举报
回复
up
stning 2009-10-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]
public static string SerializeObject(object obj)
    {
        IFormatter formatter = new BinaryFormatter();
        string result = string.Empty;
        using (MemoryStream stream = new MemoryStream())
        {
            formatter.Serialize(stream, obj);
            byte[] byt = new byte[stream.Length];
            byt = stream.ToArray();             
            result = Convert.ToBase64String(byt);
            stream.Flush();               
        }
        return result;
    }

    public static object DeserializeObject(string str)
    {
        IFormatter formatter = new BinaryFormatter();
        byte[] byt = Convert.FromBase64String(str);
        object obj = null;
        using (Stream stream = new MemoryStream(byt, 0, byt.Length))
        {
            obj = formatter.Deserialize(stream);
        }
        return obj;
    }

[/Quote]大侠,转别人的代码请注明转处
aotian798 2009-10-25
  • 打赏
  • 举报
回复
public static Message FromBytes(byte[] data)
{
using(MemoryStream ms = new MemoryStream(data,0,data.Length))
{
BinaryFormatter bf = new BinaryFormatter();
return (Message)bf.Deserialize(ms);
}
}
stning 2009-10-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
public static Message  FromBytes(byte[] data)
        {
            using(MemoryStream ms = new MemoryStream(data,0,data.Length))
            {
                BinaryFormatter bf = new BinaryFormatter();
                return (Message)bf.Deserialize(ms);
            }
        }

[/Quote]多加了个using就有用?
wuyq11 2009-10-25
  • 打赏
  • 举报
回复
public static string SerializeObject(object obj)
{
IFormatter formatter = new BinaryFormatter();
string result = string.Empty;
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
byte[] byt = new byte[stream.Length];
byt = stream.ToArray();
result = Convert.ToBase64String(byt);
stream.Flush();
}
return result;
}

public static object DeserializeObject(string str)
{
IFormatter formatter = new BinaryFormatter();
byte[] byt = Convert.FromBase64String(str);
object obj = null;
using (Stream stream = new MemoryStream(byt, 0, byt.Length))
{
obj = formatter.Deserialize(stream);
}
return obj;
}
wuyq11 2009-10-25
  • 打赏
  • 举报
回复
public static Message FromBytes(byte[] data)
{
using(MemoryStream ms = new MemoryStream(data,0,data.Length))
{
BinaryFormatter bf = new BinaryFormatter();
return (Message)bf.Deserialize(ms);
}
}
优效soft 2009-10-25
  • 打赏
  • 举报
回复
拿分
stning 2009-10-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gisyellow 的回复:]
            MemoryStream stream = new MemoryStream(bytes, 0, lenght);
            BinaryFormatter formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);
            object obj = formatter.Deserialize(stream);
            stream.Close();
            return obj as Message;
请问,你要反序列化的对象在哪儿?stream里面根本就没有数据啊!!!
[/Quote]
MemoryStream stream = new MemoryStream(bytes, 0, lenght);
这里不是给数据了吗?
gisyellow 2009-10-25
  • 打赏
  • 举报
回复
MemoryStream stream = new MemoryStream(bytes, 0, lenght);
BinaryFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj as Message;
请问,你要反序列化的对象在哪儿?stream里面根本就没有数据啊!!!
萨拉嘿 2009-10-25
  • 打赏
  • 举报
回复
序列化的对象是空的吧 反序列化才会错误,this?

formatter.Serialize(stream, this);
stning 2009-10-25
  • 打赏
  • 举报
回复
在反序列化的时候就这句出错
object obj = formatter.Deserialize(stream);
stning 2009-10-25
  • 打赏
  • 举报
回复
在反序列化的时候就这句出错
object obj = formatter.Deserialize(stream);
stning 2009-10-25
  • 打赏
  • 举报
回复
up
加载更多回复(1)

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧