Skip to content

Instantly share code, notes, and snippets.

@mjul
Created February 26, 2013 15:15
Show Gist options
  • Save mjul/5039175 to your computer and use it in GitHub Desktop.
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.
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