Thanks to Jon Skeet's answer in this question I have the following working:
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<T>(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
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 CreateDelegate
. The below is a working example that gets the error, just copy/paste into a console app.
public delegate IEnumerable<BaseItem> 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<T> GetList<T>() where T : class { return new List<T>(0); }
// not the one I want a delegate to; included for illustration
public static IEnumerable<T> GetList<T>(int param) where T : class { return new List<T>(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<BaseItem> myList = (IEnumerable<BaseItem>)getList();
}
}
As mentioned above, the only differences I can see are:
- Different return type (
T
works,IEnumerable<T>
doesn't) [EDIT: this isn't right, first version usesBaseItem
, notT
; oops] - Overloads (
GetItem
has no overloads,GetList
has several; I only need the delegate toGetList()
with no params
Update1: Sam helped me pinpoint some issues. If the return type of the delegate is generic (e.g. IEnumerable<BaseItem>
), it's choking when I try to swap base/derived types around. Is there any way I can declare my GetList
method like below? I need to be able to indicate that T
inherits from BaseItem
, but if I could then it would work fine for me.
public static IEnumerable<BaseItem> GetList<T>() where T : class
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 T
is undefined, and it won't let me use the where
constraint):
public delegate IEnumerable<T> GetListDelegate();