标签:
原文地点: 要领一使用微软官方组件Interop.MSScriptControl
1.msscript.ocx下载的地点 ?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033
或者点击此处下载 Interop.MSScriptControl.dll
2.项目中引用 msscript.ocx 或者 Interop.MSScriptControl.dll
此刻通过这个组件的一些要领来执行JS代码,并接收要领执行后的返回值,实现代码如下:
ScriptControlClass sc = new ScriptControlClass(); //申明变量 sc.UseSafeSubset = true; //允许执行不安适的代码 sc.Language = "JavaScript"; //暗示需要解析的语言为JavaScript sc.AddCode(Properties.Resources.GetTimes); // Properties.Resources 资源文件 string str = sc.Run("time", new object[] { "time()" }).ToString(); //第一个参数为要执行的要领名,第二个参数为要领体的传入参数,出格注意的是当JS要领中没有传入参数时,这时通报的为该要领名称,不填写的话会抛异常。然后将JS要领执行后的返回功效存储在变量str中
以下代码将组件的常用属性以及要领封装成单独的类,便利挪用。
using System; using MSScriptControl; using System.Text; //导入js文件,导入js 要领字符串,然后执行js要领。 通过msscript.ocx控件执行JS脚本 ,先引用 Interop.MSScriptControl.dll //官网下载: 然后 引用 xxx.ocx就行了 ?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033 namespace csharp中执行js要领 { class script_run { /// <summary> /// 脚本类型 /// </summary> public enum ScriptLanguage { /// <summary> /// JScript脚本语言 /// </summary> JScript, /// <summary> /// VBscript脚本语言 /// </summary> VBscript, /// <summary> /// JavaScript脚本语言 /// </summary> JavaScript } /// <summary> /// 脚本运行错误代办代理 /// </summary> public delegate void RunErrorHandler(); /// <summary> /// 脚本运行超时代办代理 /// </summary> public delegate void RunTimeoutHandler(); /// <summary> /// ScriptEngine类 /// </summary> public class ScriptEngine { private ScriptControl msc; /// <summary> /// 界说脚本运行错误事件 /// </summary> public event RunErrorHandler RunError; /// <summary> /// 界说脚本运行超时事件 /// </summary> public event RunTimeoutHandler RunTimeout; /// <summary> ///结构函数 默认为 VBscript 脚本类型 /// </summary> public ScriptEngine() : this(ScriptLanguage.VBscript) { } /// <summary> /// 结构函数 /// </summary> /// <param>脚本类型</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(OnError); ((DScriptControlSource_Event)this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(OnTimeout); } /// <summary> /// 运行Eval要领 /// </summary> /// <param>表达式</param> /// <param>函数体</param> /// <returns>返回值object</returns> public object Eval(string expression, string codeBody) { msc.AddCode(codeBody); return msc.Eval(expression); } /// <summary> /// 运行Eval要领 /// </summary> /// <param>脚本语言</param> /// <param>表达式</param> /// <param>函数体</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>入口函数名称 例如:add</param> /// <param>参数:例如:new object(){"",""}</param> /// <param>函数体 例如:fucniton add(int a,int b){return a+b;}</param> /// <returns>返回值object:获取object.Tostring()</returns> public object Run(string mainFunctionName, object[] parameters, string codeBody) { this.msc.AddCode(codeBody); return msc.Run(mainFunctionName, parameters); } /// <summary> /// 运行Run要领 /// </summary> /// <param>脚本语言</param> /// <param>入口函数名称</param> /// <param>参数</param> /// <param>函数体</param> /// <returns>返回值object</returns> public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody) { if (this.Language != language) this.Language = language; return Run(mainFunctionName, parameters, codeBody); } /// <summary> /// 放弃所有已经添加到 ScriptControl 中的 Script 代码和东西 /// </summary> public void Reset() { this.msc.Reset(); } /// <summary> /// 获取或设置脚本语言 /// </summary> public ScriptLanguage Language { get { return (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage), this.msc.Language, false); } set { this.msc.Language = value.ToString(); } } /// <summary> /// 获取或设置脚本执行时间,单位为毫秒 /// </summary> public int Timeout { get { return 0; } } /// <summary> /// 设置是否显示用户界面元素 /// </summary> public bool AllowUI { get { return this.msc.AllowUI; } set { this.msc.AllowUI = value; } } /// <summary> /// 宿主应用措施是否有保密性要求 /// </summary> public bool UseSafeSubset { get { return this.msc.UseSafeSubset; } set { this.msc.UseSafeSubset = true; } } /// <summary> /// RunError事件激发 /// </summary> private void OnError() { if (RunError != null) RunError(); } /// <summary> /// OnTimeout事件激发 /// </summary> private void OnTimeout() { if (RunTimeout != null) RunTimeout(); } } } } 错误谬误:
1、必需要添加微软官方组件Interop.MSScriptControl
2、不撑持开发x64的措施
要领二 操作反射获取组件(保举使用)无需引用其它dll文件,实现代码如下: