user16276760
user16276760

Reputation:

what are the type conversion and lifted call in Expression?

I'm new to ExpressionTree, below is some source code from BinaryExpression:

public class BinaryExpression : Expression
{
   internal BinaryExpression(Expression left, Expression right) 
   {
      Left = left;
      Right = right;
   }
   
   public Expression Left { get; }
   public Expression Right { get; }
   // ...
   public bool IsLifted { get; }
   public LambdaExpression Conversion => GetConversion();
   internal virtual LambdaExpression? GetConversion() => null;
 
}

I don't quite understand what are type conversion and lifted call and why we need them, can someone show me an concrete example?

Upvotes: 1

Views: 92

Answers (1)

Heinzi
Heinzi

Reputation: 172408

Lifted operator calls:

An operator call is lifted if the operator expects non-nullable operands but nullable operands are passed to it. (BinaryExpression.IsLifted)

Example: If a and b are int?, a + b is a lifted call of Int32's + operator.


Conversion:

Gets the type conversion function that is used by a coalescing or compound assignment operation. (BinaryExpression.Conversion)

This property is needed for expressions of type a ?? b, when there is an implicit user-defined conversion from a's type to b's type. A detailed answer and a concrete example can be found in this related question:

Upvotes: 1

Related Questions