This is not possible because you cannot assign an open public method to the delegate. It would be an interesting new opportunity to offer, but currently C # does not allow this.
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 workaround for the interface that you already talked about:
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());
Timwi source
share