58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using System;
|
||
using System.Reflection;
|
||
|
||
namespace CsharpHttpHelper.Helper
|
||
{
|
||
/// <summary>
|
||
/// 用户执行JS的方法 Copyright:http://www.httphelper.com/
|
||
/// </summary>
|
||
internal class ExecJsHelper
|
||
{
|
||
private static Type type = Type.GetTypeFromProgID("ScriptControl");
|
||
|
||
/// <summary>
|
||
/// 直接调用JS方法并获取返回的值
|
||
/// </summary>
|
||
/// <param name="strJs">要执行的JS代码</param>
|
||
/// <param name="main">要调用的方法名</param>
|
||
/// <returns>执行结果</returns>
|
||
internal static string JavaScriptEval(string strJs, string main)
|
||
{
|
||
object scriptControl = GetScriptControl();
|
||
SetScriptControlType(strJs, scriptControl);
|
||
return type.InvokeMember("Eval", BindingFlags.InvokeMethod, null, scriptControl, new object[1]
|
||
{
|
||
main
|
||
}).ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取ScriptControl接口类
|
||
/// </summary>
|
||
/// <param name="strJs">JS</param>
|
||
/// <param name="obj">对象</param>
|
||
/// <returns>Type</returns>
|
||
private static Type SetScriptControlType(string strJs, object obj)
|
||
{
|
||
type.InvokeMember("Language", BindingFlags.SetProperty, null, obj, new object[1]
|
||
{
|
||
"JScript"
|
||
});
|
||
type.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, obj, new object[1]
|
||
{
|
||
strJs
|
||
});
|
||
return type;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取ScriptControl接口对象
|
||
/// </summary>
|
||
/// <returns>ScriptControl对象</returns>
|
||
private static object GetScriptControl()
|
||
{
|
||
return Activator.CreateInstance(type);
|
||
}
|
||
}
|
||
}
|