I'm currently trying to generate and execute some C# code directly from a Xamarin.iOS code editor application I'm working on. I use Roslyn for all the compilation steps, but unfortunately, Mono doesn't allow you to load Assemblies at Runtime on iOS.
So, this code would typically throw a Attempting to JIT compile method while running with --aot-only
exception.
var tree = CSharpSyntaxTree.ParseText(@"public Foo
{
public void Bar() { Console.WriteLine(""""foo"""");
}");
var compilation = CSharpCompilation.Create(
"Generated." + Guid.NewGuid(),
syntaxTrees: new[] { tree },
references: references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream())
{
var result = compilation.Emit(ms);
var assembly = Assembly.Load(ms); // <- Thrown here
}
I know that Frank A. Krueger did a custom interpreter for IL for his awesome Continuous application.
I imagine having a similar approach but directly from the SemanticModel
and SyntaxTrees
outputted by Roslyn because I only want to support C#.
Regarding the pretty huge codebase of Roslyn, are there some bits I can pickup to base my interpreter on ?
Another question, without the possibility to generate Types dynamically, how could I represent those dynamic declared Types at Runtime ?
Thanks!