将列表保存到文件时出现C#错误

时间:2015-03-28 18:04:40

标签: c#

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

代码:

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();

获取错误:

  

未处理的类型&#39; System.Runtime.Serialization.SerializationException&#39;发生在mscorlib.dll

     

其他信息:键入&#39; System.Windows.Forms.MouseEventArgs&#39;在Assembly&#34; System.Windows.Forms,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089&#34;未标记为可序列化。

完整堆栈跟踪

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).

MacroEvent

[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;
        }
    }

1 个答案:

答案 0 :(得分:2)

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