Created
April 4, 2018 19:58
-
-
Save kevingosse/962a3adaa08ce5909f8b5d8b025a079d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[DllExport("compileandrun")] | |
public static void Script(IntPtr client, [MarshalAs(UnmanagedType.LPStr)] string args) | |
{ | |
if (!InitApi(client)) | |
{ | |
return; | |
} | |
try | |
{ | |
var code = File.ReadAllText(args); | |
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
using (var codeProvider = new CSharpCodeProvider()) | |
{ | |
var parameters = new CompilerParameters(new[] | |
{ | |
"mscorlib.dll", | |
"System.Core.dll", | |
"Microsoft.CSharp.dll", | |
Path.Combine(basePath, "Microsoft.Diagnostics.Runtime.dll"), | |
Path.Combine(basePath, "DynaMD.dll") | |
}); | |
parameters.GenerateInMemory = true; | |
var results = codeProvider.CompileAssemblyFromSource(parameters, code); | |
if (results.Errors.Count > 0) | |
{ | |
Console.WriteLine(string.Join("\r\n", results.Errors.Cast<CompilerError>().Select(l => l.ErrorText))); | |
return; | |
} | |
var type = results.CompiledAssembly.GetType("Test.Program"); | |
var method = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public); | |
method.Invoke(null, new object[] { Runtime.Heap }); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("An error occured: " + ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment