Exemple #1
0
        private static int?GetMultiplier(ExpressionSyntax expression, out ExpressionSyntax internalExpression)
        {
            var multiplier = 1;

            internalExpression = expression;
            var unary = internalExpression as PrefixUnaryExpressionSyntax;

            while (unary != null)
            {
                var op = unary.OperatorToken;

                if (!SupportedOperatorTokens.Contains(op.Kind()))
                {
                    return(null);
                }

                if (op.IsKind(SyntaxKind.MinusToken))
                {
                    multiplier *= -1;
                }
                internalExpression = unary.Operand;
                unary = internalExpression as PrefixUnaryExpressionSyntax;
            }

            return(multiplier);
        }
Exemple #2
0
        internal static bool TryGetConstantIntValue(ExpressionSyntax expression, out int value)
        {
            var multiplier = 1;
            var expr       = expression;
            var unary      = expr as PrefixUnaryExpressionSyntax;

            while (unary != null)
            {
                var op = unary.OperatorToken;

                if (!SupportedOperatorTokens.Contains(op.Kind()))
                {
                    value = 0;
                    return(false);
                }

                if (op.IsKind(SyntaxKind.MinusToken))
                {
                    multiplier *= -1;
                }
                expr  = unary.Operand;
                unary = expr as PrefixUnaryExpressionSyntax;
            }

            var literalExpression = expr as LiteralExpressionSyntax;

            if (literalExpression != null &&
                TryConvert(literalExpression.Token.Value, out value))
            {
                value = multiplier * value;
                return(true);
            }

            value = 0;
            return(false);
        }