Defines an interface for invoking compilation of source code or a CodeDOM tree using a specific compiler.
For a list of all members of this type, see ICodeCompiler Members.
[Visual Basic] Public Interface ICodeCompiler [C#] public interface ICodeCompiler [C++] public __gc __interface ICodeCompiler [JScript] public interface ICodeCompiler
| Class | Description |
|---|---|
| CodeCompiler | Provides an example implementation of the ICodeCompiler interface. |
The ICodeCompiler interface can be implemented for a specific compiler to enable developers to programaticaly compile assemblies from Code Document Object Model (CodeDOM) compile units, strings containing source code, or source code files.
The ICodeCompiler interface provides the capability to invoke compilation with specified parameters at runtime and access information related to compilation after compilation occurs, including the result code, and any errors or warnings the compiler returns. Each compile method accepts a CompilerParameters object that indicates settings for the compiler, and returns a CompilerResults object that indicates the results of the compilation.
Compiler developers should provide an implementation of this interface to support dynamic compilation. CodeDomProvider implementers should also consider implementing this interface to provide programmatic compilation capability for the language that they are providing CodeDom support for.
[Visual Basic, C#, C++] The following example compiles a specified source code file using the ICodeCompiler interface retrieved from a specified CodeDomProvider. For a larger code example that provides a source code model and a user interface, see the code example for the CodeDomProvider class.
[Visual Basic] Public Shared Function CompileCode(ByVal provider As CodeDomProvider, ByVal filepath As String) As CompilerResults ' Obtain an ICodeCompiler from a CodeDomProvider class. Dim compiler As ICodeCompiler = provider.CreateCompiler() ' Configure a CompilerParameters that links System.dll and ' produces a file name based on the specified source file name. Dim cp As New CompilerParameters(New String() {"System.dll"}, filepath.Substring(0, filepath.LastIndexOf(".") + 1) + "exe", False) ' Indicate that an executable rather than a .dll should be generated. cp.GenerateExecutable = True ' Invoke compilation. Dim cr As CompilerResults = compiler.CompileAssemblyFromFile(cp, filepath) ' Return the results of compilation. Return cr End Function [C#] public static CompilerResults CompileCode(CodeDomProvider provider, string filepath) { // Obtain an ICodeCompiler from a CodeDomProvider class. ICodeCompiler compiler = provider.CreateCompiler(); // Configure a CompilerParameters that links System.dll and // produces a file name based on the specified source file name. CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"}, filepath.Substring(0, filepath.LastIndexOf(".")+1)+"exe", false); // Indicate that an executable rather than a .dll should be generated. cp.GenerateExecutable = true; // Invoke compilation. CompilerResults cr = compiler.CompileAssemblyFromFile(cp, filepath); // Return the results of compilation. return cr; } [C++] public: static CompilerResults * CompileCode(CodeDomProvider* provider, String* filepath) { // Obtain an ICodeCompiler* from a CodeDomProvider class. ICodeCompiler* compiler = provider->CreateCompiler(); // Configure a CompilerParameters that links System.dll and // produces a file name based on the specified source file name. String* temp0 [] = {S"System.dll"}; CompilerParameters* cp = new CompilerParameters(temp0,String::Concat(filepath->Substring(0, filepath->LastIndexOf(S".") + 1), S"exe"), false); // Indicate that an executable rather than a .dll should be generated. cp->GenerateExecutable = true; // Invoke compilation. CompilerResults* cr = compiler->CompileAssemblyFromFile(cp, filepath); // Return the results of compilation. return cr; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Namespace: System.CodeDom.Compiler
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System (in System.dll)
ICodeCompiler Members | System.CodeDom.Compiler Namespace | CompilerParameters | CompilerResults