This is not possible because you cannot assign an open generic method to a delegate. It would be an interesting new feature to suggest, but currently C# does not allow it.
Possible workarounds:
①
delegate void Bar(object t);
void foo(Bar bar)
{
bar.Invoke("hello");
bar.Invoke(42);
}
void BarMethod(object t)
{
if (t is int)
// ...
else if (t is string)
// ...
}
foo(BarMethod);
②
delegate void Bar<T>(T t);
void foo(Bar<string> stringBar, Bar<int> intBar)
{
stringBar.Invoke("hello");
intBar.Invoke(42);
}
void BarMethod<T>(T t)
{
// ...
}
foo(BarMethod<string>, BarMethod<int>);
③
The interface workaround you already mentioned:
interface IBar
{
void Invoke<T>(T t);
}
void foo(IBar bar)
{
bar.Invoke("hello");
bar.Invoke(42);
}
class BarType : IBar
{
public void Invoke<T>(T t)
{
// ...
}
}
foo(new BarType());