System.Linq.Expressions.Expression.Condition C# (CSharp) Method

Condition() public static method

Creates a ConditionalExpression.
public static Condition ( Expression test, Expression ifTrue, Expression ifFalse ) : ConditionalExpression
test Expression An to set the property equal to.
ifTrue Expression An to set the property equal to.
ifFalse Expression An to set the property equal to.
return ConditionalExpression
        public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
        {
            RequiresCanRead(test, nameof(test));
            RequiresCanRead(ifTrue, nameof(ifTrue));
            RequiresCanRead(ifFalse, nameof(ifFalse));

            if (test.Type != typeof(bool))
            {
                throw Error.ArgumentMustBeBoolean(nameof(test));
            }
            if (!TypeUtils.AreEquivalent(ifTrue.Type, ifFalse.Type))
            {
                throw Error.ArgumentTypesMustMatch();
            }

            return ConditionalExpression.Make(test, ifTrue, ifFalse, ifTrue.Type);
        }

Same methods

Expression::Condition ( Expression test, Expression ifTrue, Expression ifFalse, Type type ) : ConditionalExpression

Usage Example

        public LambdaExpression CreateLambda(Type from, Type to)
        {
            var input    = Ex.Parameter(from, "input");
            var fromInfo = infos[from];
            var toInfo   = infos[to];

            if (fromInfo <= toInfo) // Can make use of an implicit conversion
            {
                var block  = Result(to, Ex.Convert(input, to));
                var lambda = Ex.Lambda(block, input);
                return(lambda);
            }
            else // Cannot make use of an implicit conversion, bounds must be checked. Precision might be lost.
            {
                var block = Ex.Condition(
                    Ex.MakeBinary(Et.AndAlso,
                                  Ex.MakeBinary(Et.GreaterThanOrEqual,
                                                input,
                                                Ex.Convert(Ex.Constant(toInfo.MinValue), from)),
                                  Ex.MakeBinary(Et.LessThanOrEqual,
                                                input,
                                                Ex.Convert(Ex.Constant(toInfo.MaxValue), from))),
                    Result(to, Ex.Convert(input, to)),
                    NoResult(to));
                var lambda = Ex.Lambda(block, input);
                return(lambda);
            }
        }
All Usage Examples Of System.Linq.Expressions.Expression::Condition
Expression