ConvertChecked() public static method

Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed.
/// or is null. No conversion operator is defined between .Type and .
public static ConvertChecked ( Expression expression, Type type ) : UnaryExpression
expression Expression An to set the property equal to.
type Type A to set the property equal to.
return UnaryExpression
        private UnaryExpression UnaryExpression(
            ExpressionType nodeType, System.Type type, JObject obj)
        {
            var operand = this.Prop(obj, "operand", this.Expression);
            var method  = this.Prop(obj, "method", this.Method);

            switch (nodeType)
            {
            case ExpressionType.ArrayLength: return(Expr.ArrayLength(operand));

            case ExpressionType.Convert: return(Expr.Convert(operand, type, method));

            case ExpressionType.ConvertChecked: return(Expr.ConvertChecked(operand, type, method));

            case ExpressionType.Decrement: return(Expr.Decrement(operand, method));

            case ExpressionType.Increment: return(Expr.Increment(operand, method));

            case ExpressionType.IsFalse: return(Expr.IsFalse(operand, method));

            case ExpressionType.IsTrue: return(Expr.IsTrue(operand, method));

            case ExpressionType.Negate: return(Expr.Negate(operand, method));

            case ExpressionType.NegateChecked: return(Expr.NegateChecked(operand, method));

            case ExpressionType.Not: return(Expr.Not(operand, method));

            case ExpressionType.OnesComplement: return(Expr.OnesComplement(operand, method));

            case ExpressionType.PostDecrementAssign: return(Expr.PostDecrementAssign(operand, method));

            case ExpressionType.PostIncrementAssign: return(Expr.PostIncrementAssign(operand, method));

            case ExpressionType.PreDecrementAssign: return(Expr.PreDecrementAssign(operand, method));

            case ExpressionType.PreIncrementAssign: return(Expr.PreIncrementAssign(operand, method));

            case ExpressionType.Quote: return(Expr.Quote(operand));

            case ExpressionType.Throw: return(Expr.Throw(operand, type));

            case ExpressionType.TypeAs: return(Expr.TypeAs(operand, type));

            case ExpressionType.UnaryPlus: return(Expr.UnaryPlus(operand, method));

            case ExpressionType.Unbox: return(Expr.Unbox(operand, type));

            default: throw new NotSupportedException();
            }
        }
        private Expression <Func <TBaseInput, TBaseResult> > CreatePolymorphicExpression()
        {
            // Objective:
            // x is [Mapping.InputType] ?
            // ([TResult]) [Mapping.MapperExpression { ([Mapping1.InputType]) x }]
            // : [Next Mapping OR default]

            var inputParameter = LinqExpression.Parameter(typeof(TBaseInput), "x");

            var conditionalUnits = new List <(LinqExpression condition, LinqExpression expression)>();

            foreach (var entry in _mapping.Entries)
            {
                var mappingExpression = entry.ExpressionGetter();

                // (<Mapping.InputType>) x
                var castedParameter = LinqExpression.ConvertChecked(inputParameter, entry.InputType);
                // x is <Mapping.InputType>
                var condition = LinqExpression.TypeIs(inputParameter, entry.InputType);
                // Mapper((<Mapping.InputType>) x)
                var expression = MapperInliningOperations.InlineMapperExpression(mappingExpression, castedParameter,
                                                                                 false);

                // This cast is needed to make the ternary work.
                // (TResult) Mapper((<Mapping.InputType>) x)
                expression = LinqExpression.ConvertChecked(expression, typeof(TBaseResult));

                conditionalUnits.Add((condition, expression));
            }

            // Traverse the units in reverse, we stack up the expressions as we go.
            LinqExpression?finalExpression = null;

            for (var i = conditionalUnits.Count - 1; i >= 0; i--)
            {
                var(condition, expression) = conditionalUnits[i];

                finalExpression = LinqExpression.Condition(condition,
                                                           expression,
                                                           finalExpression ?? MakeDefaultExpression.For(typeof(TBaseResult)));
            }

            return(LinqExpression.Lambda <Func <TBaseInput, TBaseResult> >(finalExpression !, inputParameter));
        }
Beispiel #3
0
 public void UnaryExpression_ConvertChecked() => ExecuteBinaryExpr(Property.Active, active => Expr.ConvertChecked(active, typeof(int)), ExpressionType.ConvertChecked);
Beispiel #4
0
 public static UnaryExpression ConvertChecked(Expression expression, Type type) => Expression.ConvertChecked(expression, type); // NB: Never binds to overload with MethodInfo parameter.