title: trouble in converting Generic List coming from a WCF Service to a DataTable
body: <p>I am confused on how can I use generic methods to parse generic list into datatable/dataset. My setup:
  1. I have a class Customers defined in WCF Service Library.</p>
  
  <pre><code>namespace Wcf.Sample.ServiceLibrary
  {
      public class Customers
      {
          public string ID = string.Empty;
          public string CompanyName = string.Empty;
          public string ContactName = string.Empty;
      }
  }
  </code></pre>
  
  <p>2. I use this class to return a generic list from my OperationContract.</p>
  
  <pre><code>namespace Wcf.Sample.ServiceLibrary
  {
      [ServiceContract]
      public interface ICustomerService
      {
          [OperationContract]
          List&lt;Customers&gt; GetAllCustomers();
      }       
  }
  </code></pre>
  
  <p>3. Consume WCF Service in web client page. On button click I populate the GridView with the list returned from GetAllCustomers(). This works perfectly fine.</p>
  
  <pre><code>GridView1.DataSource = client.GetAllCustomers();
  GridView1.DataBind();
  </code></pre>
  
  <p>4. Now the issue is, for some reason (sort/paging function) I want to actually convert the returned generic list into a datatable. To do so, I have a method that returns me a datatable which I want to bind to a GridView. Here are the methods:</p>
  
  <pre><code>public static DataTable ConvertTo&lt;T&gt;(System.Collections.Generic.List&lt;T&gt; genericList)
  {
      //create DataTable Structure
      DataTable dataTable = CreateTable&lt;T&gt;();
      Type entType = typeof(T);
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
      //get the list item and add into the list
      foreach (T item in genericList)
      {
          DataRow row = dataTable.NewRow();
          foreach (PropertyDescriptor prop in properties)
          {
              row[prop.Name] = prop.GetValue(item);
          }
          dataTable.Rows.Add(row);
      }
      return dataTable;
  }
  
  public static DataTable CreateTable&lt;T&gt;()
  {
      //T –&gt; ClassName
      Type entType = typeof(T);
      //set the datatable name as class name
      DataTable dataTable = new DataTable(entType.Name);
      //get the property list
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
      foreach (PropertyDescriptor prop in properties)
      {
          //add property as column
          dataTable.Columns.Add(prop.Name, prop.PropertyType);
      }
      return dataTable;
  }
  </code></pre>
  
  <p>I am not sure how to call this function? How can I specify the  as Customers class which is actually in a webservice? Totally lost. I would appreciate if someone can guide me on the following code, how to make it work.</p>
  
  <pre><code>GridView1.DataSource = ConvertTo&lt;???&gt;(client.GetAllCustomers());
  </code></pre>