开发者

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

开发者 https://www.devze.com 2023-03-28 06:17 出处:网络
I can\'t seem to apply binary operations to lambda expressions, delegates and method groups. dynamic MyObject = new MyDynamicClass();

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 appl开发者_StackOverflow中文版ied to operands of type 'dynamic' and 'lambda expression'

Why?

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消