C#執(zhí)行js動(dòng)態(tài)編譯的方法
本文實(shí)例講述了C#執(zhí)行js動(dòng)態(tài)編譯的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace webpro
{
public class JScripta
{
private static readonly CodeDomProvider _provider = new Microsoft.JScript.JScriptCodeProvider();
private static Type _evaluateType;
private const string scriptStr = @"package fhs
{
public class MyJs
{
public static function test1(paramr1)
{
var retString = paramr1+ '是無敵的!';
return retString;
}
}
}";
public static object JScriptRun(string jsMethodName,object[] testParams)
{
//編譯的參數(shù)
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
CompilerResults results = _provider.CompileAssemblyFromSource(parameters, scriptStr);
Assembly assembly = results.CompiledAssembly;
//動(dòng)態(tài)編譯腳本中的內(nèi)容
_evaluateType = assembly.GetType("fhs.MyJs");
//執(zhí)行指定的方法并傳參數(shù)
object retObj = _evaluateType.InvokeMember(jsMethodName, BindingFlags.InvokeMethod,
null, null, testParams);
return retObj;
}
}
}
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12C#實(shí)現(xiàn)應(yīng)用程序的監(jiān)控與調(diào)試的示例代碼
日志記錄是軟件開發(fā)中不可或缺的功能,它能幫助開發(fā)者在應(yīng)用程序運(yùn)行時(shí)記錄重要信息,本文就來介紹一下常用日志記錄功能以及常用的日志庫,感興趣的可以了解一下2024-03-03C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別
本文詳細(xì)對比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場景,希望對大家有所幫助。2016-04-04C#?winform?窗體控件跨線程訪問的實(shí)現(xiàn)
在做winform開發(fā)時(shí),如果在子線程中去設(shè)置主線程中UI控件的屬性,會(huì)出現(xiàn)“跨線程調(diào)用異常”,本文就來介紹一下C#?winform?窗體控件跨線程訪問的實(shí)現(xiàn),感興趣的可以了解一下2023-12-12c#委托把方法當(dāng)成參數(shù)(實(shí)例講解)
本篇文章主要是對c#委托把方法當(dāng)成參數(shù)的實(shí)例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#使用IronPython庫調(diào)用Python腳本
這篇文章介紹了C#使用IronPython庫調(diào)用Python腳本的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06