title: System.TypeAccessException: Attempt by method 'X' to access type 'Y' failed
body: <p>Ok, I have googled this for almost two days and I have tried almost all SO solutions that are related to this error, but nothing works. And most questions about this are for Security ssettings on Click-once applications, JSON, web applications etc. But nothing for a plain old winforms app.</p>
  
  <p>Here is the full error</p>
  
  <blockquote>
    <p>System.TypeAccessException: Attempt by method 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSystemSetup.Write3_SystemSetup(System.Object)' to access type 'DataFacture.Common.Globals+SystemSetup' failed.
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSystemSetup.Write3_SystemSetup(Object o)</p>
  </blockquote>
  
  <p>Here is a simplified version of the'SystemSetup' class</p>
  
  <pre><code>public class SystemSetup
      {
          private string machineId, windowsVersion;
  
          public SystemSetup() { }
          public SystemSetup(string machineId, string windowsVersion)
          {
              this.machineId = machineId;
              this.windowsVersion = windowsVersion
          }
  
          public string MachineID { get { return machineId; } set { machineId = value; } }
          public string WindowsVersion{ get { return windowsVersion; } set { windowsVersion= value; } }
      }
  </code></pre>
  
  <p>Now I'm trying to generate an XML of a SystemSetup object using the following code, and at the 'writer.Serialize(wfile, objectSerializer);' line, the error occurs</p>
  
  <pre><code>public static void WriteXML(Object objectSerializer, String XMLPath, String FileName)
      {
          try
          {
              if (XMLPath.Substring(XMLPath.Length - 1, 1) != @"/")
                  XMLPath = String.Format("{0}\\", XMLPath);
  
              XmlSerializer writer = null;
              Type objectType = objectSerializer.GetType();
              switch (objectType.Name)
              {
                  case "SystemSetup":
                      writer = new XmlSerializer(typeof(Globals.SystemSetup));
                      break;
              }
  
              var wfile = new System.IO.StreamWriter(String.Format("{0}{1}", XMLPath, FileName));
              writer.Serialize(wfile, objectSerializer);
              wfile.Close();
          }
          catch (Exception ex)
          {
              ErrorHandler.ShowErrorMessage(ex);
          }
      }
  </code></pre>
  
  <p>This is a winforms applications. It is not click once. And I have not enforced any security restrictions on any assembly. Also, there are no 3rd party assemblies that I'm calling from here.</p>
  
  <p><em>Edit: The above are in the same namespace but in separate class files. If I put them into one class file, it works. Not sure if that helps</em></p>