title: MethodInfo, CreateDelegate and Generic Methods
body: <p>Thanks to Jon Skeet's answer in <a href="https://stackoverflow.com/questions/307984/declare-a-generic-type-instance-dynamically">this</a> question I have the following working:</p>
  
  <pre><code>public delegate BaseItem GetItemDelegate(Guid itemID);
  
  public static class Lists
  {
    public static GetItemDelegate GetItemDelegateForType(Type derivedType)
    {
      MethodInfo method = typeof(Lists).GetMethod("GetItem");
      method = method.MakeGenericMethod(new Type[] { derivedType });
      return (GetItemDelegate)Delegate.CreateDelegate(typeof(GetItemDelegate), method);
    }
  
    public static T GetItem&lt;T&gt;(Guid itemID) where T : class { // returns an item of type T ... }
  }
  
  public class DerivedItem : BaseItem { }
  
  // I can call it like so:
  GetItemDelegate getItem = Lists.GetItemDelegateForType(typeof(DerivedItem));
  DerivedItem myItem = getItem(someID); // this works great
  </code></pre>
  
  <p>When I try to apply the same thing to a method with a different return type and overloads (those are the only differences I can come up with), I get an annoying  "ArgumentException: Error binding to target method." on the call to <code>CreateDelegate</code>. The below is a working example that gets the error, just copy/paste into a console app.</p>
  
  <pre><code>public delegate IEnumerable&lt;BaseItem&gt; GetListDelegate();
  
  public class BaseItem { }
  public class DerivedItem : BaseItem { }
  
  public static class Lists
  {
    public static GetListDelegate GetListDelegateForType(Type itemType)
    {
      MethodInfo method = typeof(Lists).GetMethod("GetList", Type.EmptyTypes); // get the overload with no parameters
      method = method.MakeGenericMethod(new Type[] { itemType });
      return (GetListDelegate)Delegate.CreateDelegate(typeof(GetListDelegate), method);
    }
  
    // this is the one I want a delegate to, hence the Type.EmptyTypes above
    public static IEnumerable&lt;T&gt; GetList&lt;T&gt;() where T : class { return new List&lt;T&gt;(0); }
    // not the one I want a delegate to; included for illustration
    public static IEnumerable&lt;T&gt; GetList&lt;T&gt;(int param) where T : class { return new List&lt;T&gt;(0); }
  
    public static Type GetItemType()
    { // this could return any type derived from BaseItem
      return typeof(DerivedItem);
    }
  }
  
  class Program
  {
    static void Main(string[] args)
    {
      Type itemType = Lists.GetItemType();
      GetListDelegate getList = Lists.GetListDelegateForType(itemType);
      IEnumerable&lt;BaseItem&gt; myList = (IEnumerable&lt;BaseItem&gt;)getList();
    }
  }
  </code></pre>
  
  <p>As mentioned above, the only differences I can see are:</p>
  
  <ol>
  <li>Different return type (<code>T</code> works, <code>IEnumerable&lt;T&gt;</code> doesn't) <em>[EDIT: this isn't right, first version uses <code>BaseItem</code>, not <code>T</code>; oops]</em></li>
  <li>Overloads (<code>GetItem</code> has no overloads, <code>GetList</code> has several; I only need the delegate to <code>GetList()</code> with no params</li>
  </ol>
  
  <p><strong>Update1:</strong> Sam helped me pinpoint some issues. If the return type of the delegate is generic (e.g. <code>IEnumerable&lt;BaseItem&gt;</code>), it's choking when I try to swap base/derived types around. Is there any way I can declare my <code>GetList</code> method like below? I need to be able to indicate that <code>T</code> inherits from <code>BaseItem</code>, but if I could then it would work fine for me.</p>
  
  <pre><code>public static IEnumerable&lt;BaseItem&gt; GetList&lt;T&gt;() where T : class
  </code></pre>
  
  <p>The other option would be to "genericize" my delegate declaration. All examples I can find use a generic for the params, not the return type. How do I do this (it throws a compiler error cause <code>T</code> is undefined, and it won't let me use the <code>where</code> constraint):</p>
  
  <pre><code>public delegate IEnumerable&lt;T&gt; GetListDelegate();
  </code></pre>