C#执行JS脚本代码的方法,拿去直接用

2018-05-12  本文已影响0人  DF_Sky

什么也不说了,一直再用,需要的直接复制过去

    /// <summary>
    /// 执行JS
    /// </summary>
    /// <param name="funcName">方法名</param>
    /// <param name="argument">传入参数</param>
    /// <param name="jsPath">JS文件路径</param>
    /// <returns></returns>
    public static object ExecuteScript(string funcName, string argument, string jsPath)
    {
        string js = System.IO.File.ReadAllText(jsPath);
        object o = ExecuteScript(string.Format("{0}('{1}')", funcName, argument), js);
        return o;
    }

    /// <summary>
    /// 执行JS
    /// </summary>
    /// <param name="sExpression">参数体</param>
    /// <param name="sCode">JavaScript代码的字符串</param>
    /// <returns></returns>
    public static object ExecuteScript(string sExpression, string sCode)
    {
        MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
        scriptControl.UseSafeSubset = true;
        scriptControl.Language = "JScript";
        scriptControl.AddCode(sCode);
        try
        {
            return scriptControl.Eval(sExpression);
        }
        catch (Exception ex)
        {

        }
        return null;
    }
上一篇下一篇

猜你喜欢

热点阅读