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 });
            }
        }
    }
  • 相关阅读:
    学习Mybatis中的一对多表关联
    学习Mybatis中的一对一表关联
    学习Mybatis中的动态sql
    学习Mybatis中的约定大于配置、数据库配置优化、定义别名、类型处理器、resultMap和parameterType
    第八周进度
    构建之法阅读笔记07
    正则表达式
    梦断代码之阅读笔记02
    顶会热词统计
    本周进度
  • 原文地址:https://www.cnblogs.com/password1/p/5870730.html
Copyright © 2011-2022 走看看