コード例 #1
0
        static bool SafeParseAndEvaluate(ProjectInfo project, MSBuildEvaluationContext context, string condition, bool collectConditionedProperties = false)
        {
            try {
                if (String.IsNullOrEmpty(condition))
                {
                    return(true);
                }

                try {
                    ConditionExpression ce = ConditionParser.ParseCondition(condition);

                    if (!ce.CanEvaluateToBool(context))
                    {
                        throw new InvalidProjectFileException(String.Format("Can not evaluate \"{0}\" to bool.", condition));
                    }

                    if (collectConditionedProperties)
                    {
                        ce.CollectConditionProperties(project.ConditionedProperties);
                    }

                    return(ce.BoolEvaluate(context));
                } catch (ExpressionParseException epe) {
                    throw new InvalidProjectFileException(
                              String.Format("Unable to parse condition \"{0}\" : {1}", condition, epe.Message),
                              epe);
                } catch (ExpressionEvaluationException epe) {
                    throw new InvalidProjectFileException(
                              String.Format("Unable to evaluate condition \"{0}\" : {1}", condition, epe.Message),
                              epe);
                }
            }
            catch {
                // The condition is likely to be invalid
                return(false);
            }
        }