title: How can I pin (and get an IntPtr to) a generic T[] array?
body: <p>I want to pin a generically-typed array and get a pointer to its memory:</p>
  
  <pre><code>T[] arr = ...;
  fixed (T* ptr = arr)
  {
      // Use ptr here.
  }
  </code></pre>
  
  <p>But trying to compile the above code produces this compiler error:</p>
  
  <pre><code>Cannot take the address of, get the size of, or declare a pointer to a managed type
  </code></pre>
  
  <p>The answers to <a href="https://stackoverflow.com/questions/7307230/pointer-to-a-array-of-type-generic">these</a> <a href="https://stackoverflow.com/questions/1631754/c-using-a-generic-to-create-a-pointer-array">questions</a> confirm that there's no way to declare a generic <code>T*</code> pointer. But is there a way to pin a generic <code>T[]</code> array and get an <code>IntPtr</code> to the pinned memory? (Such a pointer still has use because it could be passed to native code or cast to a pointer of known type.)</p>