title: C#: convert generic pointer to array
body: <p>I want to convert a <code>byte*</code> to a <code>byte[]</code>, but I also want to have a reusable function to do this:</p>
  
  <pre><code>public unsafe static T[] Create&lt;T&gt;(T* ptr, int length)
  {
      T[] array = new T[length];
  
      for (int i = 0; i &lt; length; i++)
          array[i] = ptr[i];
  
      return array;
  }
  </code></pre>
  
  <p>Unfortunately I get a compiler error because T might be a ".NET managed type" and we can't have pointers to <em>those</em>. Even more frustrating is that there is no generic type constraint which can restrict T to "unmanaged types". Is there a built-in .NET function to do this? Any ideas?</p>