Compiling .cs from .Net Applications

Created : 7/11/2022

Question

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.

Questioned by : user20620

Answer

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.

Answered by : Franci Penov