zoukankan      html  css  js  c++  java
  • c# JScriptProvider包装

    using System;
    using System.CodeDom.Compiler;
    using System.Reflection;
    using System.Web.UI;
    using Microsoft.JScript;
    
    namespace HuaTong.General.Utility
    {
        /// <summary>
        /// JScriptProvider包装
        /// </summary>
        public static class JScript
        {
            private static object js = null;
            private static Type jsType = null;
            private static readonly string jsSource =
              @"package JScript  
              {  
                 class JScript  
                 {  
                  public function Eval(expr : String) : Object  
                  {  
                   return eval(expr);  
                  }  
                 }  
              }";
            /// <summary>
            /// 构造javascript运行时对象
            /// </summary>
            static JScript()
            {
                JScriptCodeProvider compiler = new JScriptCodeProvider();
                CompilerParameters parameters = new CompilerParameters();
                parameters.GenerateInMemory = true;
                CompilerResults results = compiler.CompileAssemblyFromSource(parameters, jsSource);
    
                Assembly assembly = results.CompiledAssembly;
                jsType = assembly.GetType("JScript.JScript");
    
                js = Activator.CreateInstance(jsType);
            }
            /// <summary>
            /// 获取表达式的值
            /// </summary>
            /// <returns></returns>
            public static int EvalToInt32(string code)
            {
                string s = EvalToString(code);
                return int.Parse(s);
            }
            /// <summary>
            /// 获取表达式的值
            /// </summary>
            /// <returns></returns>
            public static double EvalToDouble(string code)
            {
                string s = EvalToString(code);
                return double.Parse(s);
            }
            /// <summary>
            /// 获取表达式的值
            /// </summary>
            /// <returns></returns>
            public static string EvalToString(string code)
            {
                object o = EvalToObject(code);
                return o.ToString();
            }
            /// <summary>
            /// 获取表达式的值
            /// </summary>
            /// <returns></returns>
            public static object EvalToObject(string code)
            {
                return jsType.InvokeMember("Eval", BindingFlags.InvokeMethod, null, js,
                      new object[] { code });
            }
        }
    }
  • 相关阅读:
    8.13实习报告
    8.10实习报告
    8.9实习报告
    8.8实习报告
    8.7实习报告
    关于线索二叉树建立和遍历
    main函数的位置可以任意
    返回指针值的函数和函数指针的区别
    runtime error: store to address 0x625000002048 with insufficient space for an object of type 'double' (solution.c) 0x625000002048: note: pointer points here
    m=-n++
  • 原文地址:https://www.cnblogs.com/password1/p/5870730.html
Copyright © 2011-2022 走看看