title: Marshal.SizeOf() for instance of boolean in C#
body: <p>I have an instance t of the following class:</p>
  <pre><code>    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
      class TestStruct
      {
          [MarshalAs(UnmanagedType.I1)] // I1 has size 1 byte
          public bool Boolean0;
          [MarshalAs(UnmanagedType.U1)] // U1 has size 1 byte
          public bool Boolean1;
      }
  
      TestStruct t = new TestStruct();
  </code></pre>
  <p>Now if I try to get access the layout of this structure for marshalling, I get inconsistent results between Marshal.OffsetOf() and Marshal.SizeOf(), as the following example shows:</p>
  <pre><code> Marshal.OffsetOf(t.GetType(), &quot;Boolean0&quot;); // returns 0 as expected
   Marshal.OffsetOf(t.GetType(), &quot;Boolean1&quot;); // returns 1 as expected
   Marshal.SizeOf(t.Boolean0) // unexpectedly returns 4!
   Marshal.SizeOf(t.Boolean1) // unexpectedly returns 4!
  </code></pre>
  <p>If found out in <a href="https://stackoverflow.com/questions/45412100/whats-the-difference-between-marshal-sizeoftypeoft-and-marshal-sizeofdefau">this question</a>, the Marshal.SizeOf() internally uses t.Boolean0.GetType() which has the default value 4 and ignores the size specified in the Attribute [MarshalAs(UnmanagedType.I1)]. This explains, why it returns 4 instead of 1.</p>
  <p>Now, is there any way I can obtain the actual unmanaged size of t.Boolean0 (1 byte in this example) by reflection?</p>