1

我正在开发一个使用 5 个不同库作为模块的项目。我的任务是使用另一个库从其中一个库中保存和加载此信息。我已经制作了保存功能,但是数据的加载/反序列化给了我一个 Inner NullReferenceException。这是堆栈跟踪:

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
   at System.RuntimeMethodHandle.SerializationInvoke(IRuntimeMethodInfo method, Object target, SerializationInfo info, StreamingContext& context)
   at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 884
   at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 283
   at System.Runtime.Serialization.ObjectManager.DoFixups() in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 935
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryobjectreader.cs:line 179
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryformatter.cs:line 197
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryformatter.cs:line 111
   at WarehouseDistribution.SimulationForm.btnLoad_Click(Object sender, EventArgs e) in C:\Users\anast\Desktop\Final Product\WarehouseDistribution\SimulationForm.cs:line 650
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WarehouseDistribution.Program.Main() in C:\Users\anast\Desktop\Final Product\WarehouseDistribution\Program.cs:line 19

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.

我查看了许多其他可能对我有帮助的线程,并尝试了诸如什么可能导致 System.TypeLoadException?BinaryFormatter.Deserialize 在 ILMerge 之后“无法找到程序集”,但没有任何效果。

以下是反序列化给定文件的方法的要点:

    IFormatter formatter = new BinaryFormatter();
    fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
    importExporter = (ImportExporter)formatter.Deserialize(fs); //loads the saved ImportExporter

    panel1.BackgroundImage = importExporter.GetMap(); //initial test

这是我想用于加载/保存的 ISerializable 类的主要部分:

    public ImportExporter(RoadManager rm, Bitmap map, Profile p)
    {
        savedRoadmanager = rm;
        savedMap = map;
        savedProfile = p;
    }

    //ISerializable constructor that loads all the needed information
    public ImportExporter(SerializationInfo info, StreamingContext context)
    {
        this.savedRoadmanager = (RoadManager)info.GetValue("savedRoadmanager", savedRoadmanager.GetType());
        this.savedMap = (Bitmap)info.GetValue("savedMap", savedMap.GetType());
        this.savedProfile = (Profile)info.GetValue("savedProfile", savedProfile.GetType());
    }


    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("savedRoadmanager", savedRoadmanager, savedRoadmanager.GetType());
        info.AddValue("savedMap", savedMap, savedMap.GetType());
        info.AddValue("savedProfile", savedProfile, savedProfile.GetType());
    }

我做了一些测试来熟悉 ISerializable 并且没有这样的问题。我觉得来源可能与该界面有关,或者可能更简单。理想情况下,我希望这不会出错,这样我就可以使用给定的数据在表单上显示信息。我已经为此工作了这么多小时,现在一切都在我的脑海中变得混乱,所以调试起来很困难,而且它对我来说是新材料。任何帮助将不胜感激。

4

0 回答 0