title: C# Error when saving a list to file
body: <p>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.</p>
  
  <h1>Code:</h1>
  
  <pre><code>public List&lt;MacroEvent&gt; events = new List&lt;MacroEvent&gt;();
  Stream stream = File.Open("Macro.bin", FileMode.Create);
  BinaryFormatter bin= new BinaryFormatter();
  bin.Serialize(stream, events);
  stream.Close();
  </code></pre>
  
  <h1>Error getting:</h1>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll</p>
    
    <p>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.</p>
  </blockquote>
  
  <h1>Full Stack Trace</h1>
  
  <pre><code>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&amp; m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message&amp; m)
     at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
     at System.Windows.Forms.Button.WndProc(Message&amp; m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; 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).
  </code></pre>
  
  <h1>MacroEvent</h1>
  
  <pre><code>[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;
          }
      }
  </code></pre>