Conrad Clark
Conrad Clark

Reputation: 4526

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

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?

Upvotes: 13

Views: 2258

Answers (2)

jbtule
jbtule

Reputation: 31799

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

Upvotes: 2

vcsjones
vcsjones

Reputation: 141638

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.

Upvotes: 13

Related Questions