title: Get list of property names using interface method
body: <p>I have two custom types <code>Customer</code> and <code>Employee</code> which implement the interface <code>ITablefy</code>.  This interface has only one method, <code>GetPropertyList</code> which returns a list of strings of the property names of the object that implements it.  I have a web service which looks like:</p>
  
  <pre><code>public string ReturnPropertyNames(ITablefy i)
  {
      List&lt;string&gt; propList = new List&lt;string&gt;();
      TableFactory factory = new TableFactory();
      ITablefy table = factory.CreateTable(i);
      propList = table.GetPropertyList(table);
      return propList[1];
  }
  </code></pre>
  
  <p>so in this example the Factory creates a concrete type that implements <code>ITablefy</code></p>
  
  <p>I realized when I had a problem when both of my classes <code>Customer</code> and <code>Employee</code> implemented their GetPropertyList methods exactly the same: </p>
  
  <pre><code>//property list is a private member variable in each class
      public List&lt;string&gt; GetPropertyList(ITablefy i)
      {
          TableFactory factory = new TableFactory();
          ITablefy table = factory.CreateTable(i);
          foreach (var propInfo in table.GetType().GetProperties())
          {
              propertyList.Add(propInfo.Name);
          }
          return propertyList;
      }
  </code></pre>
  
  <p>Rather than copy and paste that code I'm looking for a better solution to what I have currently.  If I only want certain types to use the GetPropertyList method how can I control that without having to copy and paste this same code?  Harcoding the type to create in each class doesn't seem like a good solution to me.  Employee and Customer don't logically make sense to use inheritance either.  What's a proper solution for something like this?</p>
  
  <p>factory:</p>
  
  <pre><code>public class TableFactory
  {
      public ITablefy CreateTable(ITablefy i)
      {
          if (i is Employee)
          {
              return new Employee();
          }
          else if (i is Customer)
          {
              return new Customer();
          }
          else
          {
              return null;
          }
      }
  }
  </code></pre>