Пример #1
0
 private static string ReplaceVariables(string code, IRunner runner)
 {
     Regex r = new Regex("%.*?%");
     MatchCollection matches = r.Matches(code);
     foreach (Match m in matches)
     {
         Variable v = runner.GetVariable(m.Value.Replace("%", ""));
         if (v != null)
         {
             switch (v.Type)
             {
                 case DataType.Number:
                     code = code.Replace(m.Value, (v.Value as double?).ToString());
                     break;
                 case DataType.String:
                     code = code.Replace(m.Value, "\"" + v.Value + "\"");
                     break;
                 default:
                     code = code.Replace(m.Value, v.Value.ToString());
                     break;
             }
         }
         else
         {
             throw new Exception("Unable to find variable " + m.Value.Replace("%", "") + " set during this session.");
         }
     }
     if (matches.Count == 0)
     {
         //Add double quotes if there are no variables - code evaluator needs double quotes
         if (code.Contains("+") || code.Contains("-") || code.Contains("*") || code.Contains("/") ||
             code.Contains("."))
         {
             return code;
         }
         code = "\"" + code + "\"";
     }
     return code;
 }