title: Read elements of an array of objects using C# reflection
body: <p>How can I get the list of elements and datatypes of an array of an object in c# with reflections?</p>
  
  <p><strong>Scenario:</strong> 
  I have a method with an array parameter in my web service (asmx). Using reflection I am reading the method parameters and loop through the properties. Here is my code:</p>
  
  <hr>
  
  <p><strong>Example:</strong>
  I have a webservice <a href="http://localhost/services/myservice.asmx" rel="nofollow">http://localhost/services/myservice.asmx</a>. It has a method <code>string GetServiceData(context mycontext)</code>. 
  context class has following properties</p>
  
  <pre><code>- string name
  - Account[] accounts
  </code></pre>
  
  <p>Account in turn has </p>
  
  <pre><code>- string acct_name
  - string acct_address
  </code></pre>
  
  <p>I need to read the service dynamically to generate the following output</p>
  
  <pre><code>&lt;GetServiceData&gt;
      &lt;mycontext&gt;
          &lt;name&gt;string&lt;/name&gt;
          &lt;accounts&gt;
              &lt;acct_name&gt;string&lt;/acct_name&gt;
              &lt;acct_address&gt;string&lt;/acct_address&gt;
          &lt;/accounts&gt;
      &lt;/mycontext&gt;
  &lt;/GetServiceData&gt;
  </code></pre>
  
  <p>To do so I am dynamically reading the MethodInfo and get all the parameters. Then I loop through the parameters to get the list of all the properties and the datatypes. 
  If there is an array element in the properties then I need to get all the elements of that array element.</p>
  
  <p><strong>Solution (Thanks to Ani)</strong></p>
  
  <hr>
  
  <pre><code>foreach (MethodInfo method in methods)
  {
      sb.Append("&lt;" + method.Name + "&gt;");
      ParametetInfo parameters = method.GetParameters();
      foreach(ParameterInfo parameter in parameters)
      {
          sb.Append("&lt;" + parameter.Name + "&gt;");
          if (IsCustomType(parameter.ParameterType))
          {
             sb.Append(GetProperties(parameter.ParameterType));
          }
          else
          {
              sb.Append(parameter.ParameterType.Name);
          }
          sb.Append("&lt;/" + parameter.Name + "&gt;");
      }
      sb.Append("&lt;/" + sMethodName + "&gt;");
  }
  </code></pre>
  
  <p>GetProperties() method reads the type and actually loop through each property and add it to string object. In this method I want to check if the property is an array then get all the elements and type for it.</p>
  
  <pre><code>public static string GetProperties(Type type)
  {
      StringBuilder sb = new StringBuilder();
      foreach(PropertyInfo property in type.GetProperties())
      {
          sb.Append("&lt;" + property.Name + "&gt;");
  
          if (property.PropertyType.IsArray)
          {
              Type t = property.PropertyType.GetElementType();
              sb.Append(GetProperties(t));
          }
          else
          {
              sb.Append(property.PropertyType.name);
          }
      }
      sb.ToString();
  }
  </code></pre>