zoukankan      html  css  js  c++  java
  • 关于C#执行js测试

    引入com组件

     建js 这是 掉用方法中套一个test方法,看看C#能否支持

    function  test()
    {
        return "i am test str!"
    }
    
    function sayHello(str) {
       var str= test();
       // Utf16BE.stringify("nihao")
        return "Hello," + str;
    }
     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
           private void button1_Click(object sender, EventArgs e)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "test.js";
                string str2 = File.ReadAllText(path);
    
                string fun = string.Format(@"sayHello('{0}')" ,this.textBox1.Text.Trim());
                string result = ExecuteScript(fun, str2);
    
                MessageBox.Show(result);
            }
    
            /// <summary>
            /// 执行JS
            /// </summary>
            /// <param name="sExpression">参数体</param>
            /// <param name="sCode">JavaScript代码的字符串</param>
            /// <returns></returns>
            private string ExecuteScript(string sExpression, string sCode)
            {
                MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
                scriptControl.UseSafeSubset = true;
                scriptControl.Language = "JScript";
                scriptControl.AddCode(sCode);
                try
                {
                    string str = scriptControl.Eval(sExpression).ToString();
                    return str;
                }
                catch (Exception ex)
                {
                    string str = ex.Message;
                }
                return null;
            }
        }

    点击执行

    技术交流qq群:143280841
  • 相关阅读:
    面向对象---2
    面向对象---1
    数组的复制、反转、查找(线性查找,二分法查找)
    Oracle 开放端口供客户机连接
    ORACLE常用函数大全
    ORACLE常用脚本
    C#开发实用知识点总结
    线程对话框基类
    C#开发常见问题处理
    通过修改注册表实现IE设置
  • 原文地址:https://www.cnblogs.com/zuochanzi/p/15791441.html
Copyright © 2011-2022 走看看