【问题标题】:C# Error when saving a list to file将列表保存到文件时出现 C# 错误
【发布时间】:2015-03-29 20:08:14
【问题描述】:

我创建的程序用于记录鼠标和键盘事件。但是当我创建用于保存记录并用于稍后播放的部分时,会出现以下错误。

代码:

public List<MacroEvent> events = new List<MacroEvent>();
Stream stream = File.Open("Macro.bin", FileMode.Create);
BinaryFormatter bin= new BinaryFormatter();
bin.Serialize(stream, events);
stream.Close();

获取错误:

在 mscorlib.dll 中发生了“System.Runtime.Serialization.SerializationException”类型的未处理异常

附加信息:在程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken = b77a5c561934e089”中键入“System.Windows.Forms.MouseEventArgs”未标记为可序列化。

全栈跟踪

A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
StackTrace: '   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at GlobalMacroRecorder.MacroForm.SaveMacro(Object sender, EventArgs e) in c:\Users\Cy\Desktop\Automated Setup Solution\AutomatedSetup\MacroForm.cs:line 465
   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.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   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 GlobalMacroRecorder.Program.Main() in c:\Users\Cy\Desktop\Automated Setup Solution\AutomatedSetup\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()'
The thread 0x1ad0 has exited with code 0 (0x0).

宏事件

[Serializable]
    public class MacroEvent
    {

        public MacroEventType MacroEventType;
        public EventArgs EventArgs;
        public int TimeSinceLastEvent;

        public MacroEvent(MacroEventType macroEventType, EventArgs eventArgs, int timeSinceLastEvent)
        {
            MacroEventType = macroEventType;
            EventArgs = eventArgs;
            TimeSinceLastEvent = timeSinceLastEvent;
        }
    }

【问题讨论】:

  • 打印出 full 堆栈跟踪 - 那里可能有更多信息。
  • 你能贴出 MacroEvent 类的代码吗?猜测它包含 MouseEvenArgs 类型的成员,如异常所述,它不可序列化。

标签: c#


【解决方案1】:

您不能这样做,因为 MouseEventArgs 不可序列化。您需要创建一个具有与 MouseEventArgs 类似的属性 (X,Y) 的可序列化类,然后将该类序列化为二进制文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多