cs-script

介绍可以参看  http://www.cnblogs.com/shanyou/p/3413585.html

还可以参看 这个  项目介绍 性能测试 csscript使用 

项目官网 http://www.csscript.net/FAQ.html#_alternatives

使用

using CSScriptLibrary;

int a = 1;
int b = 3;
string scriptCode = "using System;\n                        " +
                        "public class Calc                      " +
                        "{                                      " +
                        "   static public int Sum(int a, int b) " +
                        "   {                                   " +
                        "      return a + b;                    " +
                        "   }                                   " +
                        "}";


AsmHelper helper = new AsmHelper(CSScript.LoadCode(scriptCode, null, false));
int result= (int)helper.Invoke("Calc.Sum", a, b);

scriptCode 就是要执行的代码

CSScript.LoadCode 加载代码

AsmHelper 反射的帮助类

 

下面的东东卸载和删除执行后生成的assembly 文件

    string asmFile = CSScript.CompileCode(scriptCode, null, false);
    using (AsmHelper helper = new AsmHelper(asmFile, "tempDomain", true))
    {
          return (int)helper.Invoke("Calc.Sum", a, b);
    }

下面的东东 调用类的实例   calc 脚本里的类

object calc = helper.CreateObject("Calc");
return (int)helper.InvokeInst(calc, "sum", a, b);

 

对调用的方法进行限制  如静态方法

static int PrintSum(int a, int b)
{
    var printSum = CSScript.LoadMethod(
        @"public static void PrintSum(int a, int b)
          {
              Console.WriteLine((a+b));
          }")
          .GetStaticMethod();

    printSum(1, 2);
}

 

当然也可以调用文件

CSScript.Load(“hello.cs”)
 

需要注意的是   命名空间的声明和调用方法的引用   不要和原来的方法或者类发生冲突

helper.Invoke("MyNamespace.MyClass.SayHello", "Hello World!");

 

动态调用 js

可以使用这个 IronJS

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2021-10-31
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2021-09-20
  • 2021-09-15
猜你喜欢
  • 2021-12-17
  • 2021-10-11
  • 2022-12-23
  • 2021-06-08
  • 2021-11-01
  • 2021-11-21
  • 2021-10-19
相关资源
相似解决方案