title: Convert an array of pointers to an array of IntPtr
body: <p>I'm stuck on a seemingly trivial task and need your help.</p>
  
  <p>I need to write a method with the following signature:</p>
  
  <pre><code>System.Array ToIntPtrArray(System.Array a)
  </code></pre>
  
  <p>where an actual argument can be an array of any <a href="http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx" rel="noreferrer">pointer type</a> (e.g. <code>int*[]</code>, <code>long**[]</code>, <code>void*[,]</code>) and returning an array of the same shape with elements of type <a href="http://msdn.microsoft.com/en-us/library/system.intptr.aspx" rel="noreferrer"><code>System.IntPtr</code></a> having the same numeric values as elements of an input array. The problem is that I do not understand how to extract numeric values of pointers if I do not know their types beforehand.</p>
  
  <hr>
  
  <p>For example, if I knew beforehand that my argument is always of type <code>void*[]</code>, I could write the method as follows:</p>
  
  <pre><code>unsafe IntPtr[] ToIntPtrArray(void*[] a)
  {
      var result = new IntPtr[a.Length];
      for (int i = 0; i &lt; a.Length; i++)
          result[i] = (IntPtr) a[i];
  
      return result;
  }
  </code></pre>
  
  <p>But the problem is it could be not <code>void*[]</code>, but <code>void**[]</code> or anything else, and the method should be able to handle all cases.</p>