【问题标题】:Deserialize c# with BinaryFormatter使用 BinaryFormatter 反序列化 c#
【发布时间】:2015-12-28 20:01:21
【问题描述】:

我正在尝试通过序列化在 c# 中保存和加载。但是,我在加载时遇到了问题,我不确定我是否理解问题出在哪里。代码如下:

 [Serializable]
public class Network : ISerializable
{
    private static readonly string _FILE_PATH = "Network.DAT";

    //properties
    public List<Component> MyComponents { get; private set; }
    public List<Pipeline> Pipelines { get; private set; }

    public Network()
    {
        this.MyComponents = new List<Component>();
        this.Pipelines = new List<Pipeline>();
    }
    public Network(SerializationInfo info, StreamingContext context)
    {
        this.MyComponents = (List<Component>)info.GetValue("MyComponents", MyComponents.GetType());
        this.Pipelines = (List<Pipeline>)info.GetValue("Pipelines", Pipelines.GetType());
    }
    **//Methods**
    public static void SaveToFile(Network net)
    {
        using (FileStream fl = new FileStream(_FILE_PATH, FileMode.OpenOrCreate))
        {
            BinaryFormatter binFormatter = new BinaryFormatter();
            binFormatter.Serialize(fl,net );
        }
    }
    public static Network LoadFromFile()
    {
        FileStream fl = null;
        try
        {
            fl = new FileStream(_FILE_PATH, FileMode.Open);
            BinaryFormatter binF = new BinaryFormatter();
            return (Network)binF.Deserialize(fl);

        }
        catch
        {
            return new Network();
        }
        finally
        {
            if (fl != null)
            {
                fl.Close();
            }
        }
    }

   public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("MyComponents", MyComponents);

        info.AddValue("Pipelines", Pipelines);

    }

我得到的错误是:

An exception of type 'System.NullReferenceException' occurred in ClassDiagram-Final.exe but was not handled in user code

Additional information: Object reference not set to an instance of an object.

谢谢!

【问题讨论】:

    标签: c# serialization deserialization binaryformatter


    【解决方案1】:

    问题来了

    public Network(SerializationInfo info, StreamingContext context)
    {
        this.MyComponents = (List<Component>)info.GetValue("MyComponents", MyComponents.GetType());
        this.Pipelines = (List<Pipeline>)info.GetValue("Pipelines", Pipelines.GetType());
    }
    

    这就是所谓的反序列化构造函数,和任何构造函数一样,类的成员没有被初始化,所以不能使用MyComponents.GetType()Pipelines.GetType()(产生NRE)。

    你可以改用这样的东西

    public Network(SerializationInfo info, StreamingContext context)
    {
        this.MyComponents = (List<Component>)info.GetValue("MyComponents", typeof(List<Component>));
        this.Pipelines = (List<Pipeline>)info.GetValue("Pipelines", typeof(List<Pipeline>));
    }
    

    【讨论】:

    • 好吧,那行得通。我摆脱了错误。现在我正在加载保存的文件,但实际上并未加载。什么都没有改变,我的列表仍然是空的。
    • 嗯,你确定保存时它们不是空的吗?也不通过catch { return new Network(); } 分支?
    • 修复了这个问题。现在我得到这个在 mscorlib.dll 中发生了“System.Runtime.Serialization.SerializationException”类型的异常,但未在用户代码中处理附加信息:未找到成员“ComponentBox”。
    • 不幸的是,这远远超出了 OP 的范围。二进制序列化是一件棘手的事情,因为所有涉及的类都必须支持正确的序列化。您需要检查所有类(Component 和派生类,Pipeline)及其成员。我建议您激活异常设置(类似于“抛出异常时中断”)并检查调用堆栈以查看哪个类/成员抛出异常。然后查看该类的源代码等。
    • 我现在正在这样做。这是一个漫长的过程。感谢您的意见。
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2012-08-12
    • 2011-01-08
    • 2015-07-10
    相关资源
    最近更新 更多