使用ISerializable接口处理可序列化的类时,如何修复内部的NullReferenceException?

时间:2019-05-27 01:05:09

标签: c# serialization binary formatter iserializable

我正在研究一个使用5个不同库作为模块的项目。我的任务是使用另一个库来保存和加载这些信息中的一个。我已经做了保存的功能,但是数据的加载/反序列化为我提供了一个内部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.

我研究了很多其他可能对我有帮助的线程,并尝试了诸如What could be causing a System.TypeLoadException?BinaryFormatter.Deserialize "unable to find assembly" after 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,但没有出现此类问题。我觉得源可能与该接口有关,或者可能更简单。 理想情况下,我希望它不会出错,因此我可以使用给定的数据在表单上显示信息。 现在,我已经花了很多个小时来研究这个问题,以至于一切都变得混乱不堪,所以调试起来很困难,而且对我来说这是新材料。任何帮助将不胜感激。

0 个答案:

没有答案