private static Assembly GenerateAssembly(string code, AssemblyName name)
{
//Compile the source code
var references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(TabularQueryProvider).Assembly.Location),
MetadataReference.CreateFromFile(typeof(TabularTableMappingAttribute).Assembly.Location),
};
var st = CSharpSyntaxTree.ParseText(code);
var compilation = CSharpCompilation.Create(name.FullName, new[] { st }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var result = compilation.Emit(name.CodeBase);
if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
var message = string.Join("\n", failures.Select(x => $"{x.Id}: {x.GetMessage()}"));
throw new InvalidOperationException("Compilation failures!\n\n" + message + "\n\nCode:\n\n" + code);
}
var assembly = Assembly.Load(name);
return(assembly);
}