Пример #1
0
        /// <summary>
        /// Gets an expression for the string form of the supplied expression.
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        internal static IExpression GetExpressionTextExpression(IExpression expr)
        {
            string msgText = expr.ToString();

            if (msgText.StartsWith("this."))
            {
                msgText = msgText.Substring(5);
            }
            string comma = CSharpWriter.InsertSpaceAfterComma ? ", " : ",";

            if (expr is IArrayIndexerExpression)
            {
                List <IList <IExpression> > indices    = CodeRecognizer.Instance.GetIndices(expr);
                List <IExpression>          indexExprs = new List <IExpression>();
                bool foundVariableIndex = false;
                foreach (IList <IExpression> bracket in indices)
                {
                    StringBuilder bracketSb = new StringBuilder("[");
                    StringBuilder formatSb  = new StringBuilder("[");
                    for (int i = 0; i < bracket.Count; i++)
                    {
                        if (i > 0)
                        {
                            bracketSb.Append(comma);
                            formatSb.Append(",");
                        }
                        IExpression indexExpr = bracket[i];
                        bracketSb.Append(indexExpr);
                        if (indexExpr is ILiteralExpression)
                        {
                            formatSb.Append(indexExpr);
                        }
                        else
                        {
                            foundVariableIndex = true;
                            int formatIndex = indexExprs.Count;
                            formatSb.Append("{");
                            formatSb.Append(formatIndex);
                            formatSb.Append("}");
                            indexExprs.Add(indexExpr);
                        }
                    }
                    bracketSb.Append("]");
                    formatSb.Append("]");
                    msgText = msgText.Replace(bracketSb.ToString(), formatSb.ToString());
                }
                if (foundVariableIndex)
                {
                    IArrayCreateExpression indexArray = Builder.ArrayCreateExpr(typeof(object), Builder.LiteralExpr(indexExprs.Count));
                    indexArray.Initializer = Builder.BlockExpr();
                    indexArray.Initializer.Expressions.AddRange(indexExprs);
                    return(Builder.StaticMethod(new Func <string, object[], string>(string.Format), Builder.LiteralExpr(msgText), indexArray));
                }
            }
            IExpression textExpr = Builder.LiteralExpr(msgText);

            return(textExpr);
        }
Пример #2
0
        internal static IExpression MakePlaceHolderArrayCreate(IExpression expr, IList <IVariableDeclaration> indexVars)
        {
            CodeBuilder            Builder = CodeBuilder.Instance;
            IArrayCreateExpression iace    = Builder.ArrayCreateExpr(typeof(PlaceHolder), Util.ArrayInit(indexVars.Count, i => Builder.VarRefExpr(indexVars[i])));

            iace.Initializer = Builder.BlockExpr();
            iace.Initializer.Expressions.Add(expr);
            return(iace);
        }