title: System.TypeLoadException: Could not load type 'x' from assembly 'y'
body: <p>I am trying to port a program from VB6 to C# that reads in a binary file and parses it. I get no compile time errors or warnings however when I try to run it, before it even enters <code>Main()</code> it throws the exception</p>
  
  <pre><code>System.TypeLoadException was unhandled
    Message=Could not load type 'Conversion.DataStructures.ClientOld' from assembly
       'SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because
       it contains an object field at offset 1 that is incorrectly aligned or overlapped
       by a non-object field.
    Source=SandboxConsole
    TypeName=Conversion.DataStructures.ClientOld
    StackTrace:
         at sandbox.Program.Main(String[] args)
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
  </code></pre>
  
  <p>Here is a sample of the old VB6 code</p>
  
  <pre><code>Private Type SrcClientOld
      Active As String * 1            '0
      titleLength As Byte             '1
      title As String * 8             '2
      lastNameLength As Byte          '10
      LastName As String * 25         '11
      (...)
      AddedBy As String * 3           '369
      junk7 As String * 22            '372
  End Type                            '394
  </code></pre>
  
  <p>And here is my C# code I wrote</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
  struct ClientOld
  {
      [FieldOffset(0)]
      public byte Active;
  
      [FieldOffset(1)]
      [MarshalAs(UnmanagedType.AnsiBStr)]
      public string Title;
  
      [FieldOffset(10)]
      [MarshalAs(UnmanagedType.AnsiBStr)]
      public string LastName;
  
      (...)
  
      [FieldOffset(368)]
      [MarshalAs(UnmanagedType.AnsiBStr)]
      public string AddedBy;
  
      [FieldOffset(372)]
      [MarshalAs(UnmanagedType.LPArray, SizeConst = 22)]
      public byte[] Unknown7;
  }
  </code></pre>
  
  <p>After some googleing I thought that it was that I was missing the <code>Pack = 1</code> but adding that did not solve my issue. </p>
  
  <p>Any other suggestions on what to do?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The first charater is one byte long, here is a hex dump of the first record in the file</p>
  
  <pre><code>A.Dr.......Smith....................
  41 03 44 72 2E 00 00 00 00 00 05 53 6D 69 74 68 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  |  |  |                       |  ^LastName
  |  |  ^title                  ^lastNameLength
  |  ^titleLength
  ^Active
  </code></pre>
  
  <p><strong>EDIT2:</strong>
  Changing my code to the following to strip out all other possible errors it is still giving me the same exception</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
  struct ClientOld
  {
      [FieldOffset(0)]
      public byte Active;
  
      [FieldOffset(1)]
      [MarshalAs(UnmanagedType.AnsiBStr)]
      public string Title;
  }
  </code></pre>
  
  <p>I tried both fieldoffset(1) and 2 and neither work.</p>