Example #1
0
        private CodeExpression GetSubstitutedString(string text, ParameterSubstitution paramToIdentifier)
        {
            if (text == null)
            {
                return(new CodeCastExpression(typeof(string), new CodePrimitiveExpression(null)));
            }
            if (paramToIdentifier == null)
            {
                return(new CodePrimitiveExpression(text));
            }

            Regex         paramRe    = new Regex(@"\<(?<param>[^\>]+)\>");
            string        formatText = text.Replace("{", "{{").Replace("}", "}}");
            List <string> arguments  = new List <string>();

            formatText = paramRe.Replace(formatText, match =>
            {
                string param = match.Groups["param"].Value;
                string id;

                if (!paramToIdentifier.TryGetIdentifier(param, out id))
                {
                    return(match.Value);
                }

                int argIndex = arguments.IndexOf(id);

                if (argIndex < 0)
                {
                    argIndex = arguments.Count;
                    arguments.Add(id);
                }

                return("{" + argIndex + "}");
            });

            if (arguments.Count == 0)
            {
                return(new CodePrimitiveExpression(text));
            }

            var formatArguments = new List <CodeExpression> {
                new CodePrimitiveExpression(formatText)
            };

            formatArguments.AddRange(arguments.Select(id => new CodeVariableReferenceExpression(id))
                                     .Cast <CodeExpression>());

            return(new CodeMethodInvokeExpression(
                       new CodeTypeReferenceExpression(typeof(string)),
                       "Format",
                       formatArguments.ToArray()));
        }
Example #2
0
        public static CodeExpression GetSubstitutedString(this ParameterSubstitution paramToIdentifier, string text)
        {
            if (text == null)
            {
                return(new CodeCastExpression(typeof(string), new CodePrimitiveExpression(null)));
            }
            if (paramToIdentifier == null)
            {
                return(new CodePrimitiveExpression(text));
            }
            var            regex     = new Regex("\\<(?<param>[^\\>]+)\\>");
            var            input     = text.Replace("{", "{{").Replace("}", "}}");
            var            arguments = new List <string>();
            MatchEvaluator evaluator = match =>
            {
                if (!paramToIdentifier.TryGetIdentifier(match.Groups["param"].Value, out string id))
                {
                    return(match.Value);
                }
                int num = arguments.IndexOf(id);
                if (num < 0)
                {
                    num = arguments.Count;
                    arguments.Add(id);
                }
                return("{" + num + "}");
            };
            string str2 = regex.Replace(input, evaluator);

            if (arguments.Count == 0)
            {
                return(new CodePrimitiveExpression(text));
            }
            var codeExpressionList = new List <CodeExpression> {
                new CodePrimitiveExpression(str2)
            };

            codeExpressionList.AddRange(arguments.Select(id => new CodeVariableReferenceExpression(id)).Cast <CodeExpression>());
            return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(string)), "Format", codeExpressionList.ToArray()));
        }