DynamicObject Implicit Casting

I have a subclass of DynamicObject, and I would like to implement implicit casting for primitive types similar to DO by TryConvert direct casting; that is, without writing down several implicit functions of the [type] operator.

Using:

dynamic myDynamicObject = new MyDynamicObject("1");
int sum = 1 + myDynamicObject; // instead of int i = 1 + (int)myDynamicObject;

Is this possible, and if so, how?

+5
source share
2 answers

Here are a few things going on.

First you perform a binary operation. So you need to override the TryBinaryOperation method . It will be called first before converting. Then, from the TryBinaryOperation method, you can perform the conversion.

-, - TryBinaryOperation , :

int sum = myDynamicObject + 1;

, , . DLR, .

Update: . "1 + myDynamicObject" "myDynamicObject + 1" TryBinaryOperation, - TryBinaryOperationFromRight, DynamicObject .

+5

DLR , , DO .

: " , , ".

TryBinaryOperation, (+, -,...).

+3

All Articles