title: Suggestions on Working with this Inherited Generic Method
body: <p>We have inherited a project that is a wrapper around a section of the core business model. </p>
  
  <p>There is one method that takes a generic, finds items matching that type from a member and then returns a list of that type.</p>
  
  <pre><code>public List&lt;T&gt; GetFoos&lt;T&gt;()
  {
      List&lt;IFoo&gt; matches = Foos.FindAll(
          f =&gt; f.GetType() == typeof(T)
      );
  
      List&lt;T&gt; resultList = new List&lt;T&gt;();
      foreach (var match in matches)
      {
          resultList.Add((T)obj);
      }
  }
  </code></pre>
  
  <p>Foos can hold the same object cast into various classes in inheritance hierarchy to aggregate totals differently for different UI presentations.  There are 20+ different types of descendants that can be returned by GetFoos.</p>
  
  <p>The existing code basically has a big switch statement copied and pasted throughout the code.  The code in each section calls GetFoos with its corresponding type.  </p>
  
  <p>We are currently refactoring that into one consolidated area, but as we are doing that we are looking at other ways to work with this method.</p>
  
  <p>One thought was to use reflection to pass in the type, and that worked great until we realized the Invoke returned an object, and that it needed to be cast somehow to the List &lt;T&gt;.</p>
  
  <p>Another was to just use the switch statement until 4.0 and then use the dynamic language options.</p>
  
  <p>We welcome any alternate thoughts on how we can work with this method.  I have left the code pretty brief, but if you'd like to know any additional details please just ask.</p>
  
  <p><strong>Update</strong></p>
  
  <p>The switch statement was originally using strings and the first pass was moving it into a Presenter with something like this: (No refactoring done on the switch, just where the data went).</p>
  
  <pre><code>// called on an event handler in FooPresenter
  // view is the interface for the ASPX page injected into FooPresenter's constructor
  // wrapper is the instance of the model wrapper that has the GetFoos method
  // selectedFooName is the value of a DropDownList in the Page
  //  letting the user say how they want to see the animals
  //   its either one big group (Animal)
  //   or individual types (Tiger, Lion)
  private void LoadFoos(string selectedFooName)
  {
      switch (selectedFooName)
      {
          case "Animal":  // abstract base class for all other types
              this.view.SetData(this.wrapper.GetFoos&lt;Animal&gt;(); 
  
          case "Lion":
              this.view.SetData(this.wrapper.GetFoos&lt;Lion&gt;();
              break;
  
          case "Tiger":
              this.view.SetData(this.wrapper.GetFoos&lt;Tiger&gt;();    
              break;
  
          case "Bear":
              this.view.SetData(this.wrapper.GetFoos&lt;Bear&gt;();
              break;    
      }
  }
  </code></pre>
  
  <p>The View implementation (codebehind for an ASPX page)</p>
  
  <pre><code>public void SetData&lt;T&gt;(List&lt;T&gt; data)
  {
      // there is a multiview on the page that contains user controls with 
      // grid layouts for the different types
  
      // there is a control for the common type of "Animal"
      // and other controls for Tiger, Bear, etc
  
      // the controls contain a 3rd party grid of pain 
      // and the grids' binding event handlers cast the data item 
      // for the row and do some stuff with it specific to that type
  }
  </code></pre>
  
  <p>Our first pass was going to be at least using the Type in the switch statement, or adding an enum.</p>
  
  <p>I played around with using the Strategy Pattern but had to stop when I got to the loading factory returning the List again and realizing I didn't have the type.</p>