Is it possible to pass a method group as an argument in C#?
I am using a lot of mocks. I have the following:
public static List<IInvocation> InvocationsWithName<T>(
this Mock<T> mock, string methodName)
where T: class
{
var invocations = mock.Invocations
.Where(i => i.Method.Name == methodName)
.ToList();
return invocations;
}
I use it like this:
var invocations = myInterface
.InvocationsWithName(nameof(MyInterface.MyMethod));
It works. But I would prefer to use it like this:
var invocations = myInterface
.InvocationsWithName(MyInterface.MyMethod);
To do that, I need to define my InvocationsWithName
extension method in a way that accepts MyInterface.MyMethod
as the argument. But I don't know if/how that can be done.
In practice, MyMethod might be a single method, or it might be a method group with multiple members. Ideally, the solution would work in both cases.