解决方案 »

  1.   

    http://www.cnblogs.com/tianyamoon/archive/2008/01/26/1053905.html条件判断应该没有问题,但要实现Then里面的东西,要改改。
      

  2.   


    public static float GetInterest(int Q)
    {
        if (Q <= 40 || Q >= 450)
        {
            throw new ArgumentOutOfRangeException("Q is invalid");
        }
        float Y = 0.9F;
        if (Q >= 100)
        {
            Y += 0.02F;
        }
        return Y;
    }
      

  3.   


    public class CsharpStringEvalEngine
        {
            /// <summary>
            /// C#表达式计算 注意区分大小写
            /// </summary>
            /// <param name="expression">普通的C#字符串表达式</param>
            /// <returns></returns>
            public object Eval(string expression)
            {
                string CLASSNAME = "Calc";
                string METHODNAME = "Run";            //创建编译器实例。  
                CSharpCodeProvider complier = new CSharpCodeProvider();            //设置编译参数。  
                CompilerParameters cp = new CompilerParameters();
                cp.ReferencedAssemblies.Add("System.dll");
                cp.ReferencedAssemblies.Add("system.data.dll");
                cp.ReferencedAssemblies.Add("system.xml.dll");
                cp.GenerateExecutable = false;
                cp.GenerateInMemory = true;            //创建动态代码。  
                StringBuilder code = new StringBuilder();
                code.Append("using System; \n");
                code.Append("using System.Text; \n");
                code.Append("using System.Text.RegularExpressions; \n");
                code.Append("using System.Collections; \n");
                code.Append("using System.Collections.Generic; \n");
                code.Append("using System.Collections.Specialized; \n");
                code.Append("using System.Data; \n");
                code.Append("using System.Xml; \n");
                code.Append("public  class  " + CLASSNAME + "\n");
                code.Append("{\n");
                code.Append("        public  object  " + METHODNAME + "()\n");
                code.Append("        {\n");
                code.Append("                return  " + expression + ";\n");
                code.Append("        }\n");
                code.Append("}");
                
                //编译代码。  
                CompilerResults cr = complier.CompileAssemblyFromSource(cp, code.ToString());
                
                if (cr.Errors.HasErrors)
                {
                    StringBuilder error = new StringBuilder();
                    error.Append("Error Compiling Expression: ");                foreach (CompilerError err in cr.Errors)
                    {
                        error.AppendFormat("{0}\n", err.ErrorText);
                    }                throw new Exception("Error Compiling Expression: " + error.ToString());
                }
                else
                {
                    //获取编译后的程序集。  
                    Assembly assembly = cr.CompiledAssembly;                //动态调用方法。  
                    object eval = assembly.CreateInstance(CLASSNAME);
                    MethodInfo method = eval.GetType().GetMethod(METHODNAME);
                    
                    return method.Invoke(eval, null);
                }
            }
        }动态编译。以上以前写的代码仅供参考。
    注意这些地方:
     code.Append("        public  object  " + METHODNAME + "()\n");
     code.Append("        {\n");
     code.Append("                return  " + expression + ";\n");
      code.Append("        }\n");当然,还有其他办法,自己可以研究下。
      

  4.   

    谢谢,挺有用的,由于处理的语句有点多,所以我就对if的条件语句动态编译得到true和false的结果了。
      

  5.   

    恩。最后我就只进行条件判断了,then里的语句按公式的模式处理就好了。