当前位置:编程学习 > JAVA >>

C#字符串表达式计算,后台javascript脚本计算

一直以来一直使用js引擎,也就是MSScript.ocx即Interop.MSScriptControl.dll来实现字符串表达式的计算。

但是周一在部署时发现存在64位不支持的问题,虽然开启32位后解决了,但是心里总是个疙瘩。

昨天利用C#代码编译功能终于解决了C#字符串表达式的计算的问题,方法和代码都来自互联网,非常感谢,也把自己修改后的分享给大家

一、脚本方法

using System;
using System.Collections.Generic;
using System.Text;
using MSScriptControl;

namespace MyQuery.CSharpScript
{
    /// <summary>
    /// 脚本运行错误代理
    /// </summary>
    public delegate void RunErrorHandler();

    /// <summary>
    /// 脚本运行超时代理
    /// </summary>
    public delegate void RunTimeoutHandler();

    /// <summary>
    /// 脚本处理引擎
    /// by 开源盛世-源代码下载网 基于网络文章整理开发,是学习自定义脚本的产物
    /// </summary>
    public sealed class ScriptEngine
    {
        private ScriptControlClass msc;
        /// <summary>
        /// 定义脚本运行错误事件
        /// </summary>
        public event RunErrorHandler RunError;
        /// <summary>
        /// 定义脚本运行超时事件
        /// </summary>
        public event RunTimeoutHandler RunTimeout;

        /// <summary>
        ///构造函数
        /// </summary>
        public ScriptEngine()
            : this(ScriptLanguage.JavaScript)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="language">脚本类型</param>
        public ScriptEngine(ScriptLanguage language)
        {
            this.msc = new ScriptControlClass();
            this.msc.UseSafeSubset = true;
            this.msc.Language = language.ToString();
            ((DScriptControlSource_Event)this.msc).Error += new DScriptControlSource_ErrorEventHandler(ScriptEngine_Error);
            ((DScriptControlSource_Event)this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout);
        }

        /// <summary>
        /// 运行Eval方法
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <param name="codeBody">函数体</param>
        /// <returns>返回值object</returns>
        public object Eval(string expression, string codeBody)
        {
            msc.AddCode(codeBody);
            return msc.Eval(expression);
        }

        /// <summary>
        /// 运行Eval方法
        /// </summary>
        /// <param name="language">脚本语言</param>
        /// <param name="expression">表达式</param>
        /// <param name="codeBody">函数体</param>
        /// <returns>返回值object</returns>
        public object Eval(ScriptLanguage language, string expression, string codeBody)
        {
            if (this.Language != language)
                this.Language = language;
            return Eval(expression, codeBody);
        }

        /// <summary>
        /// 运行Run方法
        /// </summary>
        /// <param name="mainFunctionName">入口函数名称</param>
        /// <param name="parameters">参数</param>
        /// <param name="codeBody">函数体</param>
        /// <returns>返回值object</returns>
        public object Run(string mainFunctionName, object[] parameters, string codeBody)
        {
            this.msc.AddCode(codeBody);
            return msc.Run(mainFunctionName, parameters);
        }

        /// <summary>
        /// 运行Run方法
        /// </summary>
        /// <param name="language">脚本语言</param>
        /// <param name="mainFunctionName">入口函数名称</param>
        /// <param name="parameters">参数</param>
        /// <param name="codeBody">函数体</param>
        /// <returns>返回值object</returns>
        public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody)
        {
            if (this.Language != language)
       &nb

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,