Dispatch recursive generic function call based on array parameter type
<p>Writing a function which for a given type behaves differently depending on whether the parameter is an array of arbitrarily deep dimensions, or a non-array (value). For array types, the overload iterates over the array and recurses.</p>

<p>I would like the recursion to call the array parameter overload for recursive calls to sub-arrays. However instead the recursive call goes straight to the value type overload. I would prefer to avoid runtime based functionality such as introspection.</p>

<p>Using Visual Studio 2010 .Net 4 although would like to support .Net 3.5 as well.</p>

<pre><code>namespace Quickie.StackOverflow.GenericMethodDispatch
{
    class TestGenericMethodDispatch
    {
        static private void GenericDereferenceArray&lt;T&gt;(T t)
        {
            Console.WriteLine(string.Format("GenericDereferenceArray&lt;T&gt;(T t) got a {0} which thinks it's a {1}, {2}", typeof(T), t.GetType(), t));
        }

        static private void GenericDereferenceArray&lt;T&gt;(T[] t)
        {
            Console.WriteLine(string.Format("GenericDereferenceArray&lt;T&gt;(T[] t) got a {0} which thinks it's a {1}", typeof(T[]), t.GetType()));
            foreach (T iter in t)
            {
                GenericDereferenceArray(iter);
            }
        }

        static public void TestGenericDereferenceArray()
        {
            Console.WriteLine();
            Console.WriteLine("===TestGenericDereferenceArray()===");

            Console.WriteLine("====Dereference an int====");
            int i = 1;
            GenericDereferenceArray(i);
            Console.WriteLine();

            Console.WriteLine("====Dereference an int[]====");
            int[] ai = { 1 };
            GenericDereferenceArray(ai);
            Console.WriteLine();

            Console.WriteLine("====Dereference an int[][]====");
            int[][] aai = { new int[] { 2 } };
            GenericDereferenceArray(aai);
            Console.WriteLine();
        }
    } // Ends class TestGenericMethodDispatch
} // Ends namespace Quickie.StackOverflow.GenericMethodDispatch
</code></pre>

<p>Output:</p>

<blockquote>
  <p>===TestGenericDereferenceArray()===<br>
  ====Dereference a string====<br>
  GenericDereferenceArray(T t) got a System.String which thinks it's a System.String, foo  </p>
  
  <p>====Dereference an int====<br>
  GenericDereferenceArray(T t) got a System.Int32 which thinks it's a System.Int32, 1  </p>
  
  <p>====Dereference an int[]====<br>
  GenericDereferenceArray(T[] t) got a System.Int32[] which thinks it's a System.Int32[]<br>
  GenericDereferenceArray(T t) got a System.Int32 which thinks it's a System.Int32, 1  </p>
  
  <p>====Dereference an int[][]====<br>
  GenericDereferenceArray(T[] t) got a System.Int32[][] which thinks it's a System.Int32[][]<br>
  GenericDereferenceArray(T t) got a System.Int32[] which thinks it's a System.Int32[], System.Int32[]  </p>
</blockquote>

<p>Hoping the latter output would have been</p>

<blockquote>
  <p>====Dereference an int[][]====<br>
  GenericDereferenceArray(T[] t) got a System.Int32[][] which thinks it's a System.Int32[][]<br>
  GenericDereferenceArray(T[] t) got a System.Int32[] which thinks it's a System.Int32[]<br>
  GenericDereferenceArray(T t) got a System.Int32 which thinks it's a System.Int32, System.Int32  </p>
</blockquote>
