Created
February 26, 2013 15:15
-
-
Save mjul/5039175 to your computer and use it in GitHub Desktop.
Compile and call JavaScript code from .NET (C#).
Opens up the possibility to use for example ClojureScript core.logic as a rule engine in C# business applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CodeDom.Compiler; | |
using Microsoft.JScript; | |
namespace JavascriptFromDotNet | |
{ | |
class Program | |
{ | |
private const string Source = @" | |
package Test | |
{ | |
class HelloWorld | |
{ | |
function Hello(name) { return ""Hello, "" + name; } | |
} | |
}"; | |
static void Main(string[] args) | |
{ | |
var provider = new JScriptCodeProvider(); | |
var compiler = provider.CreateCompiler(); | |
var parameters = new CompilerParameters {GenerateInMemory = true}; | |
var results = compiler.CompileAssemblyFromSource(parameters,Source); | |
var assembly = results.CompiledAssembly; | |
dynamic instance = Activator.CreateInstance(assembly.GetType("Test.HelloWorld")); | |
var result = instance.Hello("World"); | |
Console.WriteLine("Result: {0}", result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment