Ejemplo n.º 1
0
 //计算表达式,错误返回null
 /// <summary>
 /// 计算表达式,错误返回null
 /// </summary>
 /// <param name="expression">表达式</param>
 /// <param name="argument">参数</param>
 /// <param name="num">参数值</param>
 /// <returns></returns>
 public static string Eval(string expression, string argument, string num)
 {
     MSScriptControl.ScriptControl script = new MSScriptControl.ScriptControlClass();
     script.Language = "JavaScript";
     try
     {
         // return (new SimpleExpressionEvaluator.ExpressionEvaluator().Evaluate(ReplaceExpressionWithArguement(expression, argument, num))).ToString();
         return(script.Eval(ReplaceExpressionWithArguement(expression, argument, num)).ToString());
     }
     catch (Exception)
     {
         return(null);
     }
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            //string f1 = "";
            //A:基本工资
            //B:绩效系数
            MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
            sc.Language = "JavaScript";
            string formulate2 = string.Format("if({0}==1)({2});else if({0}==2)({3});else if({0}==3)({4})", 3, 2, 3, 4, 5);
            //string formulate1 = string.Format("if({0}==2)((({0}*{1})-{3}+({1}*{2}))+{4}/{0});else(1+2*3)", 2, 3, 4, 5, 6);
            object objResult = sc.Eval(formulate2);

            if (objResult != null)
            {
                Console.WriteLine(objResult);
            }
            Console.ReadKey();
        }