开发者

DeSerializing a List not working

I'm saving a list of my class using serialization. This part works perfectly, when I try to deserialize however, it doesn't do anything.

private class Company : ISerializable
{
    public string Name;
    public System.IO.FileSystemWatcher Watch;
    public string Email;
}

The method:

public Company(SerializationInfo info, StreamingContext ctxt)
{
    Name = (String)info.GetValue("Name", typeof(string));
    Watch.Path = (String)info.GetValue("Watch Path", typeof(string));
    Email = (String)info.GetValue("Email address", typeof(string));
}

And finally the serialization itself:

Stream stream = File.Open(User + ".osl", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, CompanyList);
stream.Close();

CompanyList is the list holding the Company class objects. When I check the file it is all there perfectly. However, when I try to deserialize to the CompanyList it doesn't work. It goes through without an error it just doesn't give back any informatio开发者_如何学编程n.

I only just started using serialization so I'm sure I messed up something.

Edit Deserialization code:

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Name", Name);
            info.AddValue("Watch Path", Watch.Path);
            info.AddValue("Email address", Email);
        }

It looks like this. Then I just call it casting it as a List of Companies: (List<Company>)bformatter.Deserialize(stream);


It is likely that the deserialization constructor throws an exception because the FileSystemWatcher is null.

You're setting the Path property of the Watch object, but you don't initialize the actual Watch object, at least not in the code you have shown here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜