简体   繁体   English

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

[英]C# Error when saving a list to file

Program that I create is use for recording the mouse and keyboard events. 我创建的程序用于记录鼠标和键盘事件。 But when I create the part for saving the record and use for play it back later the error below occur. 但是,当我创建用于保存记录的声部并稍后用于回放时,会出现以下错误。

Code: 码:

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

Error getting: 错误获取:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.Runtime.Serialization.SerializationException'的未处理异常

Additional information: Type 'System.Windows.Forms.MouseEventArgs' in Assembly "System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken = b77a5c561934e089" is not marked as serializable. 附加信息:在程序集“ System.Windows.Forms,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089”中,键入“ System.Windows.Forms.MouseEventArgs”未标记为可序列化。

Full Stack Trace 全栈跟踪

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

You cannot do that, because the MouseEventArgs is not serializable. 您不能这样做,因为MouseEventArgs无法序列化。 You will need to create a serializable class with properties (X,Y) similar to that of MouseEventArgs and then serialize that class to binary file. 您将需要创建一个具有类似于MouseEventArgs的属性(X,Y)的可序列化类,然后将该类序列化为二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM