title: Size of struct with generic type fields
body: <p>I want to estimate the size of an array of structs containing generic type parameters, in this case a dictionary entry struct. To do that I need the size of the struct.</p>
  
  <pre><code>struct Entry
  {
     int hash;
     int next;
     TKey key;
     TValue value;
  }
  </code></pre>
  
  <p>How can I get the size in bytes of this struct? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>It seems using <code>Marshal.SizeOf</code> is problematic. Passing the <em>type</em> of the struct will raise an exception saying that the argument can't be a generic type definition.</p>
  
  <p>If I instead call the overload that takes an instance, e.g. <code>Marshal.SizeOf(default(Entry))</code> it will work if <em>both</em> generic type arguments are value types. If the generic arguments are e.g. <code>&lt;int, object&gt;</code> then this exception is thrown</p>
  
  <blockquote>
    <p>Dictionary`2+Entry[System.Int32,System.Object]' cannot be marshaled as
    an unmanaged structure; no meaningful size or offset can be computed.</p>
  </blockquote>