This is my scenario:
Class Test
{
public int TestMethod(string s)
{
return s.length;
}
public void TestInvoker()
{
var result = Invoker.Call( (new Test()).TestMethod,"String");
}
}
Class Invoker
{
public static object Call(Delegate method, object input) {... Do stuff ...}
}
How can I do this? because “TestMethod” isn’t a Delegate, i could do it with Func<>, but I want to avoid instatiate a Func<> delegate each time i use the invoker.
Is this possible?
It is possible, but only if the method accepts a concrete delegate type; not
Delegateitself.If you change your method to
, it will work fine.
To be more precise, C# has an implicit conversion from a method group to a matching delegate type.