Ejemplo n.º 1
0
        public static bool IsNull(ExpressionNode expression)
        {
            ConstantExpression constantExpression = expression as ConstantExpression;

            return(constantExpression != null && constantExpression.IsNullValue);
        }
Ejemplo n.º 2
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			base.VisitMethodInvocationExpression(expression);

			// Constant folding must not be applied if the method is non-deterministic.
			if (!expression.Method.IsDeterministic)
				return expression;
			
			// Check if target is a constant or even null.
			
			ConstantExpression targetAsConstant = expression.Target as ConstantExpression;
			
			if (targetAsConstant == null)
				return expression;
			
			if (targetAsConstant.IsNullValue)
                return LiteralExpression.FromTypedNull(expression.ExpressionType);
			
			// Check if all arguments are constants or at least one argument
			// is null.

			ConstantExpression[] constantArguments = new ConstantExpression[expression.Arguments.Length];
			bool allArgumentsAreConstants = true;

			for (int i = 0; i < constantArguments.Length; i++)
			{
				constantArguments[i] = expression.Arguments[i] as ConstantExpression;

				if (constantArguments[i] == null)
				{
					// Ok, one argument is not a constant
					// But don't stop: If an argument is null we can still calculate the result!
					allArgumentsAreConstants = false;
				}
				else if (constantArguments[i].IsNullValue)
				{
					// We found a null. That means the invocation will also yield null.
                    return LiteralExpression.FromTypedNull(expression.ExpressionType);
				}
			}

			if (allArgumentsAreConstants)
			{
				try
				{
                    return LiteralExpression.FromTypedValue(expression.GetValue(), expression.ExpressionType);
				}
				catch (RuntimeException ex)
				{
					_errorReporter.CannotFoldConstants(ex);
				}
			}

			return expression;
		}