Compile Source Code Programmatically

Problem

You want to compile code from a string or a source file using a custom .NET program.

Solution

Use the Microsoft.VisualBasic.VBCodeProvider to create an ICodeCompiler object.

Discussion

The .NET Framework allows you to access the Microsoft Visual Basic, Visual C#, Visual J#, and JScript language compilers. To compile code using one of these engines, you call the CreateCompiler method of the appropriate code provider class. (In the case of Visual Basic .NET, this class is Microsoft.VisualBasic.VBCodeProvider.) The CreateCompiler method returns an ICodeCompiler object that allows you to create assemblies in memory or on disk.

Compiling code can be a painstaking task. You need to ensure that you supply all the ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.