title: C# reflection: getting the values of an array
body: <p>I'm trying to write a reflection class (for custom serialization).
  I'm stumped on the syntax for iterating through an array.
  The following works for non-arrays, but I'm missing the array part, and couldn't puzzle it out from other answers here (some of which do not compile)...</p>
  
  <p>To implement deserialization, write-able references to each object in the target class must be obtained in order to set their values from the previously-serialized data.</p>
  
  <p>Thanks in advance for any tips !</p>
  
  <pre><code>    private static void diagPrint(Object val)
      {
          if (val == null)
              return; // whoops
          Type t = val.GetType();
          string r = "";
          if (t.IsArray)
          {
              Type t_item = t.GetElementType();
              r += "Type=" + t_item.Name + "[], value=[";
              //
              // ??? OK, now what ? How to iterate through the elements of the array ???
              // Needs to work for arrays of simple type like Bool or arrays of objects...
              //
              r += "]";
          }
          else
          {
              r += "Type=" + t.Name + ", value=";
              r += val.ToString();
          }
          MessageBox.Show(r);
      }
  </code></pre>