手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#执行js动态编译的方法
C#执行js动态编译的方法
摘要:本文实例讲述了C#执行js动态编译的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:usingSystem;usingSys...

本文实例讲述了C#执行js动态编译的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:using System;

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)

{

//编译的参数

CompilerParameters parameters = new CompilerParameters();

parameters.GenerateInMemory = true;

CompilerResults results = _provider.CompileAssemblyFromSource(parameters, scriptStr);

Assembly assembly = results.CompiledAssembly;

//动态编译脚本中的内容

_evaluateType = assembly.GetType("fhs.MyJs");

//执行指定的方法并传参数

object retObj = _evaluateType.InvokeMember(jsMethodName, BindingFlags.InvokeMethod,

null, null, testParams);

return retObj;

}

}

}

希望本文所述对大家的C#程序设计有所帮助。

【C#执行js动态编译的方法】相关文章:

C#中如何执行存储过程方法

C#获取系统版本信息方法

C#操作目录与文件的方法步骤

C#中控制远程计算机的服务的方法

C# 批处理调用方法

c#解压文件的实例方法

c# asp .net 动态创建sql数据库表的方法

C# 打开电子邮件软件的具体方法

PowerShell 定时执行.Net(C#)程序的方法

c# 获取数据库中所有表名称的方法

精品推荐
分类导航