I have a variable of type Func<dynamic>
and I am trying to assign it a value. If I assign it to a method that returns a value type (e.g. int
), I get the error
'int MethodName()' has the wrong return type
If I wrap the method in a lambda call, however, it works fine. Also methods that return reference types seem to work fine.
private string Test()
{
return "";
}
private int Test2()
{
return 0;
}
Func<dynamic> f = Test; // Works
Func<dynamic> g = Test2; // Does not
Func<dynamic> h = () => Test2(); // Works
What's wrong with the direct assignment case?
dynamic
. If you change yourFunc<dynamic>
toFunc<object>
you get the same error, and I think it's because boxing operation would get introduced indirectly, and compiler is not ok with that, but I can't find anything in the spec that would describe that situation. – Wealth