title: C# Reflection: Getting the fields of a DataRow from a Typed DataSet
body: <p>I am currently building a method that takes an object that is of type <code>DataRow</code> from a typed DataSet, and then returning a string in <a href="http://en.wikipedia.org/wiki/Json" rel="nofollow noreferrer">JSON</a> format of the fields in the DataRow (for use in a Web Service).</p>
  
  <p>By using <code>System.Reflection</code>, I am doing something like this : </p>
  
  <pre><code>public string getJson(DataRow r)
      {
          Type controlType = r.GetType();
          PropertyInfo[] props = controlType.GetProperties();
          foreach (PropertyInfo controlProperty in props)
          {
  
          }
          return "";
      }
  </code></pre>
  
  <p>And then in the <code>foreach</code> statement, I would iterate every field and get the field name and value, and format it into JSON.</p>
  
  <hr>
  
  <p>The problem is that when iterating over the <code>props</code> (of type <code>PropertyInfo[]</code>), I am getting properties that I do not want to be iterated over:</p>
  
  <p><a href="http://img88.imageshack.us/img88/2001/datarowreflectionht0.gif" rel="nofollow noreferrer">alt text http://img88.imageshack.us/img88/2001/datarowreflectionht0.gif</a></p>
  
  <p>As you can see from the above image, I only need the fields that range from <code>0 - 11</code> in the <code>props</code> array, because those are the 'real fields' of this particular typed row.</p>
  
  <p>So my question is, <strong>How can I get the fields of the Typed DataRow only, and not the other 'metadata' ?</strong></p>
  
  <hr>
  
  <p><strong>[UPDATE with Solution]</strong></p>
  
  <p>As <a href="https://stackoverflow.com/questions/431050/c-reflection-getting-the-fields-of-a-datarow-from-a-typed-dataset#431058">Mehrdad Afshari</a> suggested, instead of using <code>Reflection</code>, I am using the <code>Table.Columns</code> array.  </p>
  
  <p>Here is the completed function:</p>
  
  <pre><code>public string GetJson(DataRow r)
  {
      int index = 0;
      StringBuilder json = new StringBuilder();
      foreach (DataColumn item in r.Table.Columns)
      {
          json.Append(String.Format("\"{0}\" : \"{1}\"", item.ColumnName, r[item.ColumnName].ToString()));
          if (index &lt; r.Table.Columns.Count - 1)
          {
              json.Append(", ");
          }
          index++;
      }
      return "{" + json.ToString() + "}";
  }
  </code></pre>