Is there a managed API to compile C# applications (to memory or disk)? I assume one exists because SharePoint compiles ASP.NET pages in memory. We want to use it to, at runtime, generate Linq DataContexts and compile those against various projects.
2 Answers
Take a look at the Microsoft.CSharp.CSharpCodeProvider class. That page has sample code how to use that class to compile C# sources.
Note: This class cannot be use in partial trust. You need to run with full trust in order to be able to run the C# compiler.
Comments
As Franci has pointed out, the CSharpCodeProvider is what you want.
While writing my C# book I built "Snippy" which is a quick'n'dirty way of building and running snippets of C# - it's a simple enough project to make it quite easy to see how to use CSharpCodeProvider. The full source is available - take a look.