title: How can I marshal a pointer to an array of structures as an array if the size of the array is contained in one of the structure fields?
body: <p>I want to transfer one of the structures from the DXGI library, but I ran into a problem. The target structure contains a pointer to an array of structures, in the second after, where the first indicates the size of this array. And I would not want to work directly with a pointer to an array, but with the help of a marshalller, do what is required.</p>
  
  <p>I tried to do it like this:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct PresentParameters
  {
      public uint DirtyRectsCount { get; set; }
      [field: MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Struct, SizeParamIndex = 0)]
      public Rect[] DirtyRects { get; set; }
      [field: MarshalAs(UnmanagedType.LPStruct)]
      public Rect ScrollRect { get; set; }
      [field: MarshalAs(UnmanagedType.LPStruct)]
      public Point ScrollOffset { get; set; }
  }
  </code></pre>
  
  <p>But the original structure looks like this:</p>
  
  <pre><code>typedef struct DXGI_PRESENT_PARAMETERS
  {
      UINT DirtyRectsCount;
      /* [annotation] */ 
      _Field_size_full_opt_(DirtyRectsCount)  RECT *pDirtyRects;
      RECT *pScrollRect;
      POINT *pScrollOffset;
  } DXGI_PRESENT_PARAMETERS;
  </code></pre>
  
  <p>Is it possible to do this, or is it worth describing a custom marshaller who would do this?</p>