Just ran into this today
An anonymous function or method group cannot be used as a constituent value of a dynamically bound operation.
What is the static type of the lamba a => a.address_1
? You may be tempted to say it's a Func
. But remember:
A lambda expression is an anonymous function that you can use to create delegates or expression tree types.
So maybe it's an Expression
. A lamda by itself doesn't have a single static type.
Now normally type inference would figure out that you're passing the lamba to a function that takes a Func
and it will be converted to a delegate at compile time. However when you are calling with dynamic arguments the method call is dispatched dynamically.
If you have a method call with a dynamic argument, it is dispatched dynamically, period. During the runtime binding, all the static types of your arguments are known (emphasis mine), and types are picked for the dynamic arguments based on their actual values.
So the fact that your method takes a Func
isn't take into account, since the actual method call isn't determined until runtime so there is no type inference.
To get this to compile you'll have to cast your lamba to a Func
as below:
return ifNotNull(firstAddress, new Func((a) => a.address_1), null);
Now the static type of your lamda is known.