If I attempt to write two overloads of a method, one accepting an Expression<Func<T>>
parameter and another accepting a Func<T>
, I will get a compiler error on trying to call the method with a lambda expression because the two signatures create ambiguity. The following would be problematic, for example:
Method(() => "Hello"); // Is that a Func<string>,
// or is it an Expression<Func<string>>?
I get that. But I don't like the approach of just accepting an Expression<Func<string>>
, as this forces calling code to use a lambda expression. What if I want to be able to accept a method group as well?
My basis for asking this question is that I'm working on some code where I'd like to be able to write something like this:
Method(() => "Hello");
...and get output like this:
Executed '() => "Hello"' and got "Hello" back.
Meanwhile, I'd like to be able to do this as well:
Method(ReturnHello);
...and get output like this:
Executed 'ReturnHello' and got "Hello" back.
Is there any way to do what I'm trying to do here, without just using two different method names (e.g., ExpressionMethod
and FuncMethod
)? I realize that wouldn't be such a big deal; I'm just curious if there's another way.