title: How do I write a C# method to return a Func<T> based in an enum parameter?
body: <p>I have a set of methods each of which return an ObservableCollection for various types of T, and would like to be able to write a factory method that returns these methods, based on an enum value sent in. For example, suppose I have the following highly realistic methods...</p>
  
  <pre><code>public ObservableCollection&lt;Gribble&gt; GetGribbles() {
    return new ObservableCollection&lt;Gribble&gt;();
  }
  
  public ObservableCollection&lt;Gribulator&gt; GenerateGribulators() {
    return new ObservableCollection&lt;Gribulator&gt;();
  }
  
  public ObservableCollection&lt;Knepple&gt; MakeKnepples() {
    return new ObservableCollection&lt;Knepple&gt;();
  }
  </code></pre>
  
  <p>...and the following enum...</p>
  
  <pre><code>public enum ListNames {
    Gribbles,
    Gribulators,
    Knepple
  }
  </code></pre>
  
  <p>Then I would like to be able to do this...</p>
  
  <pre><code>Func&lt;ObservableCollection&lt;Gribble&gt;&gt; someGribbles = 
         MakeFunc&lt;Gribble&gt;(ListNames.Gribbles);
  </code></pre>
  
  <p>The reason for this is that I have a helper class with a signature like this...</p>
  
  <pre><code>public void GetList&lt;T&gt;(ListNames listName, 
    Func&lt;ObservableCollection&lt;T&gt;&gt; serviceCall, 
    Action&lt;ObservableCollection&lt;T&gt;&gt; handleList)
  </code></pre>
  
  <p>serviceCall is the method that gets the list, and handleList is a callback method. At the moment, wherever this method is called, the code has to pass the actual function that gets the list.</p>
  
  <p>However, this is proving to be a bad design choice, as it means that if two parts of the code both make a call to GetList for a particular list, they could pass in two different service calls, which will cause inconsistent results. I want to encapsulate the service call in the MakeFunc method, which would be called from GetList, meaning that the code that uses GetList never sees the actual service call, and so can't do anything wrong.</p>
  
  <p>I have tried a few things, but am struggling to get anything that will compile. The closest I have got is the following...</p>
  
  <pre><code>public Func&lt;ObservableCollection&lt;T&gt;&gt; MakeFunc&lt;T&gt;(ListNames listName) {
    switch (listName) {
      case ListNames.Gribbles:
        return GetGribbles;
      case ListNames.Gribulators:
        return GenerateGribulators;
      case ListNames.Knepple:
        return MakeKnepples;
      default:
        throw new ArgumentException("Unknown list name");
    }
  }
  </code></pre>
  
  <p>...but this gives a compiler errors along the lines of "<em>GetGribbles() has the wrong return type</em>" and the tooltip that pops up when I hover my mouse over GetGribbles in the MakeFunc method says </p>
  
  <blockquote>
    <p>Expected a method with <code>'ObservableCollection&lt;T&gt; GetGribbles()'</code> signature</p>
  </blockquote>
  
  <p>Is it possible to do what I want? If so, anyone able to explain what I did wrong?</p>
  
  <p>I'm using C# 4.0 in Visual Studio 2010 if it makes any difference.</p>