title: Making Extension Methods/Constructors of a wrapper class Generic
body: <p>In a service (<em>that I cannot alter</em>) I have two object classes <code>Bar</code> and <code>Baz</code> with mostly similar properties (<em>but sadly NO, they don't derive from the same base class or inherit from the same Interface... yeah -- dumb</em>), as well as a dependency class related to their relative <code>BarQux</code> and <code>BazQux</code> properties:</p>
  
  <pre><code>public class Bar                          public class Baz
  {                                         {
      public int ID { get; set; }               public int ID { get; set; }
      public bool Active { get; set; }          public bool Active { get; set; }
      public int BarQux { get; set; }           public int BazQux { get; set; }
      ...                                       ...
  }                                         }
  
  public class Qux
  {
      public int ID { get; set; } // Corresponds to BarQux and BazQux
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>In a WPF screen, I am binding a list of each type (<code>Baz</code> and <code>Bar</code>) to two seperate ListViews. I need each to have an additional <code>Selected</code> CheckBox column. To do so, I've created a wrapper class with the common properties, the additional <code>Selected</code> property, and a constructor for each:</p>
  
  <pre><code>public class Foo
  {
      public Foo(Bar bar, Qux qux)
      {
          this.Active = bar.Active;
          this.FooQux = string.Format("{0} - {1}", qux.ID, qux.Name);
          ...
      }
  
      public Foo(Baz baz, Qux qux)
      {
          this.Active = baz.Active;
          this.FooQux = string.Format("{0} - {1}", qux.ID, qux.Name);
          ...
      }
  
      public bool Selected { get; set; }
      public int ID { get; set; }
      public bool Active { get; set; }
      public string FooQux { get; set; }
      ...
  }
  </code></pre>
  
  <p>To convert each collection of class <code>Baz</code> and <code>Bar</code> to collections of <code>Foo</code>, I created the following extension methods:</p>
  
  <pre><code>public static List&lt;Foo&gt; ToFoo(this IEnumerable&lt;Bar&gt; bars, IEnumerable&lt;Qux&gt; quxs)
  {
      List&lt;Foo&gt; foos = new List&lt;Foo&gt;();
  
      foreach (Bar bar in bars)
      {
          Foo foo = new Foo(bar, quxs.Single(qux =&gt; qux.ID == bar.BarQux));
          foos.Add(foo);
      }
  
      return foos;
  }
  
  public static List&lt;Foo&gt; ToFoo(this IEnumerable&lt;Baz&gt; bazs, IEnumerable&lt;Qux&gt; quxs)
  {
      List&lt;Foo&gt; foos = new List&lt;Foo&gt;();
  
      foreach (Baz baz in bazs)
      {
          Foo foo = new Foo(baz, quxs.Single(qux =&gt; qux.ID == baz.BazQux));
          foos.Add(foo);
      }
  
      return foos;
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>How do I make this generic?</p>
  
  <p><strong>Theory, Implementation, and Error:</strong></p>
  
  <ol>
  <li><p>Since the constructors are virtually the same besides the <code>Bar</code> and <code>Baz</code> parameters, can I somehow use generic type <code>T</code> to make one constructor and still grab the properties?</p>
  
  <pre><code>public class Foo&lt;T&gt;
  {
      public Foo(T obj, Qux qux)
      {
          this.Active = obj.Active; // 'T' does not contain a definition for 'Active'...
          this.Qux = string.Format("{0} - {1}", qux.ID, qux.Name);
          ...
      }
      ...
  }
  </code></pre></li>
  <li><p>Change the constructors to receive the whole collection of <code>Qux</code> objects and do the <code>quxs.Single(qux =&gt; qux.ID == object.ObjectQux)</code> logic there. Then make the extension methods into one generic method, something like the following.</p>
  
  <pre><code>public static List&lt;Foo&gt; ToFoo&lt;T&gt;(this IEnumerable&lt;T&gt; objCollection, IEnumerable&lt;Qux&gt; quxs)
  {
      List&lt;Foo&gt; foos = new List&lt;Foo&gt;();
  
      foreach (T obj in objCollection)
      {
          Foo foo = new Foo(obj, quxs); // The best overloaded method... has some invalid arguments.
          foos.Add(foo);
      }
  
      return foos;
  }
  </code></pre></li>
  <li><p>Both 1 and 2 combined? Anything I haven't thought of?</p></li>
  </ol>