コード例 #1
0
        public void Ctor_checks_else_result_type()
        {
            var ex = Assert.Throws <ArgumentException>(
                () => new CaseExpression(
                    new[] { new CaseWhenClause(Expression.Constant(false), Expression.Constant(false)) },
                    Expression.Constant(0)));

            Assert.Contains(
                RelationalStrings.CaseElseResultTypeUnexpected(typeof(int), typeof(bool)),
                ex.Message);
            Assert.Equal("elseResult", ex.ParamName);
        }
コード例 #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="CaseExpression" /> class.
        /// </summary>
        /// <param name="operand"> The case operand expression. </param>
        /// <param name="whenClauses"> The list of when clauses. </param>
        /// <param name="elseResult"> The else result expression. </param>
        public CaseExpression(
            [CanBeNull] Expression operand,
            [NotNull] IReadOnlyList <CaseWhenClause> whenClauses,
            [CanBeNull] Expression elseResult)
        {
            Check.NotEmpty(whenClauses, nameof(whenClauses));

            var resultType = whenClauses[0].Result.Type;
            var expectedWhenOperandType = operand?.Type ?? typeof(bool);

            foreach (var whenClause in whenClauses)
            {
                if (operand != null &&
                    whenClause.Test.Type != expectedWhenOperandType)
                {
                    throw new ArgumentException(
                              RelationalStrings.CaseWhenClauseTestTypeUnexpected(
                                  whenClause.Test.Type,
                                  expectedWhenOperandType),
                              nameof(whenClauses));
                }

                if (whenClause.Result.Type != resultType)
                {
                    throw new ArgumentException(
                              RelationalStrings.CaseWhenClauseResultTypeUnexpected(whenClause.Result.Type, resultType),
                              nameof(whenClauses));
                }
            }

            if (elseResult != null &&
                elseResult.Type != resultType)
            {
                throw new ArgumentException(
                          RelationalStrings.CaseElseResultTypeUnexpected(elseResult.Type, resultType),
                          nameof(elseResult));
            }

            Type        = resultType;
            Operand     = operand;
            ElseResult  = elseResult;
            WhenClauses = whenClauses;
        }