Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator 'op ' cannot be applied to operands of type 'dynamic' and 'lambda expression'

Tags:

c#

dynamic

I can't seem to apply binary operations to lambda expressions, delegates and method groups.

dynamic MyObject = new MyDynamicClass();
MyObject >>= () => 1 + 1;

The second line gives me error: Operator '>>=' cannot be applied to operands of type 'dynamic' and 'lambda expression'

Why?

Isn't the operator functionality determined by my custom TryBinaryOperation override?

like image 245
Conrad Clark Avatar asked Aug 12 '11 16:08

Conrad Clark


2 Answers

It's not an issue with MyDynamicClass, the problem is that you can't have a lambda expression as a dynamic. This however, does appear to work:

dynamic MyObject = new MyDynamicClass();
Func<int> fun = () => 1 + 1;
var result = MyObject >>= fun;

If the TryBinaryOperation looks like this:

result = ((Func<int>) arg)();
return true;

Then result will be 2. You can use binder.Operation to determine which binary operation this is.

like image 149
vcsjones Avatar answered Nov 14 '22 17:11

vcsjones


dynamic MyObject = new MyDynamicClass();
MyObject >>= new Func<int>(() => 1 + 1);
like image 45
jbtule Avatar answered Nov 14 '22 17:11

jbtule