using MSScriptControl;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using v8sharp;
namespace Js调用
{
class Program
{
static void Main(string[] args)
{
string reString = @"
function MessageBox(s)
{
return 'Hellow ' + s;
}
";
string MethodName = "MessageBox";
string para = "'bbs.msdn5.com'";
//请先引用 Interop.MSScriptControl.dll Framework4.0 引用 Interop.MSScriptControl_fr40
// string info =RunMethod(reString,para,MethodName); //Run
//string info = EvalMethod(reString, para, MethodName); //Eval
string info = V8Method(reString, para, MethodName); //V8
Console.WriteLine(info);
Console.ReadKey();
}
/// <summary>
/// 执行js V8方法
/// </summary>
/// <param name="reString">Js代码</param>
/// <param name="para">参数字符串(使用逗号分隔)</param>
/// <param name="MethodName">方法名称</param>
static string V8Method(string reString, string para, string MethodName)
{
V8Engine engine = V8Engine.Create();//创建V8对象
V8Script script = engine.Compile(reString);//编译
try
{
engine.Execute(script);//将编译的脚本加载到V8引擎中
string res = engine.Execute(string.Format("{0}({1})", MethodName, para)).ToString();//执行结果
return res;
}
catch (Exception ex)
{
return ex.Message;//异常信息
}
}
/// <summary>
/// 执行js Eval方法
/// </summary>
/// <param name="reString">Js代码</param>
/// <param name="para">参数字符串(使用逗号分隔)</param>
/// <param name="MethodName">方法名称</param>
static string EvalMethod(string reString, string para, string MethodName)
{
try
{
Type obj = Type.GetTypeFromProgID("ScriptControl");
if (obj == null) return "";
//使用默认构造函数创建对象实例
object ScriptControl = Activator.CreateInstance(obj);
// InvokeMember 设置属性
obj.InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JScript" });
obj.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { reString });
//执行当前对象的方法(方法名) InvokeMethod 异步执行某一个方法
return obj.InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { string.Format("{0}({1})", MethodName, para) }).ToString();//执行结果
}
catch (Exception ex)
{
string ErrorInfo = string.Format("执行JS出现错误: \r\n 错误描述: {0} \r\n 错误原因: {1} \r\n 错误来源:{2}", ex.Message, ex.InnerException.Message, ex.InnerException.Source);//异常信息
return ErrorInfo;
}
}
/// <summary>
/// 执行js Run方法
/// </summary>
/// <param name="reString">Js代码</param>
/// <param name="para">参数字符串(使用逗号分隔)</param>
/// <param name="MethodName">方法名称</param>
static string RunMethod(string reString, string para, string MethodName)
{
ScriptControlClass sc = new ScriptControlClass();
try
{
sc.UseSafeSubset = true;//大家自行查询下具体代表的含义.
sc.Language = "JScript"; //Asp vbScritp JScript!= JavaScript
sc.AddCode(reString);
string[] str = para.Split(',');
object[] obj = new object[str.Length];
; i < str.Length; i++)
{
obj[i] = str[i];
}
return sc.Run(MethodName, obj).ToString(); //执行结果
}
catch (Exception ex)
{
if (sc.Error.Source != null)
{
string txt = sc.Error.Text == null ? "" : sc.Error.Text;
string ErrorInfo = string.Format("执行JS出现错误: \r\n 第{0}行,第{1}列, \r\n 错误描述: {2}: {3};\r\n 错误Code:{4}", sc.Error.Line.ToString(), sc.Error.Column.ToString(), sc.Error.Source, sc.Error.Description.ToString() + txt, sc.Error.Number.ToString());
// ErrorInfo 异常信息
return txt + ErrorInfo;
}
else
{
// ex.Message; 异常信息
return ex.Message;
}
}
}
}
}