Search code examples
c#.netruntime

How do I allow my .net program to compile C# code while running?


I've seen a couple .net applications that allow you to select a C# .cs source file and the program itself will compile this code and run it.

How is this done?


Solution

  • It probably uses the CSharpCodeProvider class in the Microsoft.CSharp namespace. You would want to look at the following methods to compile code:

    CompileAssemblyFromDom
    CompileAssemblyFromFile
    CompileAssemblyFromSource