I have a method on an interface:
Someclass DoSomething(Someclass whatever);
I can't do .Returns(x => x)
because of the error:
cannot convert lambda expression to type Someclass because it is not a delegate type.
Any ideas?
Take a look at these method overloads:
Returns(TResult value)
Returns<T>(Func<T, TResult> valueFunction)
It seems quite obvious for you that C# compiler should pick second overload. But actually C# compiler is unable to infer type of generic parameter T
from argument x => x
which you are passing. That's why compiler picks non-generic version of method and tries to convert lambda to SomeClass.
To fix this issue you should help compiler to infer type of generic parameter T
. You can do it by explicitly specifying type of delegate argument:
.Returns((SomeClass x) => x);
Or you can specify type of T
manually
.Returns<SomeClass>(x => x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With