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

ConvertChecked() public static method

Creates a UnaryExpression that represents a conversion operation that throws an exception if the target type is overflowed and for which the implementing method is specified.
/// or is null. /// is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument. More than one method that matches the description was found. No conversion operator is defined between .Type and .-or-.Type is not assignable to the argument type of the method represented by .-or-The return type of the method represented by is not assignable to .-or-.Type or is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by .
public static ConvertChecked ( Expression expression, Type type, MethodInfo method ) : UnaryExpression
expression Expression An to set the property equal to.
type Type A to set the property equal to.
method System.Reflection.MethodInfo A to set the property equal to.
return UnaryExpression
        public static UnaryExpression ConvertChecked(Expression expression, Type type, MethodInfo method)
        {
            RequiresCanRead(expression, nameof(expression));
            ContractUtils.RequiresNotNull(type, nameof(type));
            TypeUtils.ValidateType(type, nameof(type));
            if (type.IsByRef)
            {
                throw Error.TypeMustNotBeByRef(nameof(type));
            }

            if (type.IsPointer)
            {
                throw Error.TypeMustNotBePointer(nameof(type));
            }

            if (method == null)
            {
                if (expression.Type.HasIdentityPrimitiveOrNullableConversionTo(type))
                {
                    return new UnaryExpression(ExpressionType.ConvertChecked, expression, type, null);
                }
                if (expression.Type.HasReferenceConversionTo(type))
                {
                    return new UnaryExpression(ExpressionType.Convert, expression, type, null);
                }
                return GetUserDefinedCoercionOrThrow(ExpressionType.ConvertChecked, expression, type);
            }
            return GetMethodBasedCoercionOperator(ExpressionType.ConvertChecked, expression, type, method);
        }

Same methods

Expression::ConvertChecked ( Expression expression, Type type ) : UnaryExpression

Usage Example

        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();
            }
        }
All Usage Examples Of System.Linq.Expressions.Expression::ConvertChecked
Expression