简体   繁体   中英

Operator with C# dynamic?

I have this function:

static void Func1<T>(T x, T y)
{
    dynamic result = ((dynamic)x + y); //line 1
    dynamic result2 = (x + y);         //line 2
}

This func can be executed as Func(1,2); However, line 1 is OK , while line 2 goes BANG (at compile time).

The exception thrown from line 2 is:

Operator '+' cannot be applied to operands of type 'T' and 'T'

So, we need to create an operator overload. Okay, so far so good.

But what about line 1? Shouldn't it need a dynamic cast also on y ?

((dynamic)x + (dynamic)y);

I understand that it is being evaluated at runtime, but why does the C# compiler accept the + operator in line 1 (ie wrongly assume that T can be + to something else)?

In your first example, by making x a dynamic you've in effect made the operator+ operation dynamic as well. This gets rid of the type specifier T for x , thereby getting rid of the complaint that T has no valid operator+ .

At run time dynamic binding will occur and evaluate the two operands to ensure that operator+ can be used:

If an operand of an arithmetic operator has the compile-time type dynamic, then the expression is dynamically bound (§7.2.2). In this case the compile-time type of the expression is dynamic, and the resolution described below will take place at run-time using the run-time type of those operands that have the compile-time type dynamic.

In your second example, the compiler knows the types for x + y and is simply storing the result into a dynamic variable. Further usages of result2 will be dynamically bound. This makes sense as there are no dynamic operations to the right of the assignment operator:

When no dynamic expressions are involved, C# defaults to static binding, which means that the compile-time types of constituent expressions are used in the selection process.

dynamic basically tells the compiler "don't try to make sure what I'm doing is legal; I'm sure it will be at runtime". Any operation you attempt on a variable of dynamic type will compile. It just won't run successfully if the type assigned to the dynamic variable doesn't actually implement the operation.

As for why both of them don't have to be dynamic, the compiler will basically try to find an operator (static method) on either of the types involved in the operation that matches the signature, starting with the LValue. With the LValue being dynamic, the compiler must assume the operation exists on anything that will be used as X, even though X is of the same placeholder type as Y and Y is not known to have a + operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM