给出一个表达式以及表达式里面所有变量的值。求出这个表达式的值。类似javascript中的Eval().

    技术2022-05-11  120

    给出一个表达式以及表达式里面所有变量的值。求出这个表达式的值。类似javascript中的Eval().

    作者:timmy3310

    <script type="text/javascript"> <!-- google_ad_client = "pub-0523096416305353"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as_rimg"; google_cpa_choice = "CAAQycb8zwEaCNrmdvgKt1bFKJnA93M"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-0523096416305353&cpa_choice=CAAQycb8zwEaCNrmdvgKt1bFKJnA93M&dt=1135325720770&lmt=1135325720&format=468x60_as_rimg&output=html&url=http://www.faq-it.org/archives/csharp/831bd1a95c55c31d2e26441cd324bd6b.php®ion=_google_cpa_region_&cc=100&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=11&u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="65535">

    Runtime  C#  Expression  Evaluator   http://www.codeproject.com/csharp/runtime_eval.asp   ---------------------------------------------------------------     http://expert.csdn.net/Expert/topic/1553/1553098.xml?temp=.1884882     请参考     这个帖子里面有我写的一个表达式解析类       ---------------------------------------------------------------     using  System;   using  System.CodeDom;   using  System.CodeDom.Compiler;   using  Microsoft.CSharp;   using  System.Reflection;   using  System.Text;     namespace  TestApp   {          public  class  Test          {                  public  static  void  Main()                    {                          string  expression  =  "125.8  +  208.2  +  120.90";                          string  ret  =  Eval.Calc(expression).ToString();                              Console.Write("{0}  =  {1}  ",expression,    ret);                  }          }            public  class  Eval            {                  public  static  object  Calc(string  expression)                  {                          string  className  =  "Calc";                          string  methodName  =  "Run";                                                    //  创建编译器实例。                          ICodeCompiler  complier  =  (new  CSharpCodeProvider().CreateCompiler());                          //  设置编译参数。                          CompilerParameters  paras  =  new  CompilerParameters();                          paras.GenerateExecutable  =  false;                          paras.GenerateInMemory  =  true;                            //  创建动态代码。                          StringBuilder  classSource  =  new  StringBuilder();                            classSource.Append("public  class  "+  className  +"/n");                          classSource.Append("{/n");                          classSource.Append("        public  object  "  +  methodName  +  "()/n");                          classSource.Append("        {/n");                          classSource.Append("                return  "+  expression  +  ";/n");                          classSource.Append("        }/n");                          classSource.Append("}");                            System.Diagnostics.Debug.WriteLine(classSource.ToString());                            //  编译代码。                          CompilerResults  result  =  complier.CompileAssemblyFromSource(paras,  classSource.ToString());                                                    //  获取编译后的程序集。                          Assembly  assembly  =  result.CompiledAssembly;                              //  动态调用方法。                          object  eval  =  assembly.CreateInstance(className);                          MethodInfo  method  =  eval.GetType().GetMethod(methodName);                          return  method.Invoke(eval,  null);                  }          }   }  

    最新回复(0)