-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (51 loc) · 1.7 KB
/
Copy pathProgram.cs
File metadata and controls
63 lines (51 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// .NET JScript execution example
// Author: MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
using System;
using System.Collections.Generic;
using Microsoft.JScript;
using System.CodeDom.Compiler;
using System.Text;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static object JsExecute(
string script,
string[] refs,
string type,
string func,
params object[] args)
{
JScriptCodeProvider jsc = new JScriptCodeProvider();
CompilerParameters parameters = new CompilerParameters( refs, "test.dll", true);
parameters.GenerateExecutable = false;
CompilerResults results = jsc.CompileAssemblyFromFile(parameters, new string[] { script });
if (results.Errors.Count > 0)
{
Console.WriteLine("Errors: ");
foreach (CompilerError err in results.Errors)
{
Console.WriteLine(err.ToString());
}
return null;
}
Assembly ass = results.CompiledAssembly;
Type c = ass.GetType(type);
MethodInfo f = c.GetMethod(func);
return f.Invoke(null, args);
}
static void Main(string[] args)
{
object res = JsExecute(
"test.js",
new[] { "mscorlib.dll", "System.Core.dll" },
"C",
"calc_sum",
new object[] { 1, 2 }
);
Console.WriteLine(res);
Console.Read();
}
}
}