Export (0) Print
Expand All

ICodeCompiler Interface

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

Classes that Implement ICodeCompiler

Class Description
CodeCompiler Provides an example implementation of the ICodeCompiler interface.

Remarks

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.

Example

[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 sourceFile As string,        _
                                   ByVal exeFile As string) As CompilerResults
    ' Obtain an ICodeCompiler from the CodeDomProvider.
    Dim compiler As ICodeCompiler = provider.CreateCompiler()

    ' Configure a CompilerParameters that links System.dll
    ' and produces the specified executable file.
    Dim referenceAssemblies As String() = {"System.dll"}
    Dim cp As New CompilerParameters(referenceAssemblies, exeFile, False)
    
    ' Generate an executable rather than a DLL file.
    cp.GenerateExecutable = True

    ' Invoke compilation.
    Dim cr As CompilerResults = compiler.CompileAssemblyFromFile(cp, _
        sourceFile)
    ' Return the results of compilation.
    Return cr
End Function 

[C#] 
public static CompilerResults CompileCode(CodeDomProvider provider, 
                                          String sourceFile, 
                                          String exeFile)
{
    // Obtain an ICodeCompiler from the CodeDomProvider.
    ICodeCompiler compiler = provider.CreateCompiler();
    // Configure a CompilerParameters that links System.dll
    // and produces the specified executable file.
    String [] referenceAssemblies = {"System.dll"};
    CompilerParameters cp = new CompilerParameters(referenceAssemblies,
                                                   exeFile, false);
    // Generate an executable rather than a DLL file.
    cp.GenerateExecutable = true;            

    // Invoke compilation.
    CompilerResults cr = compiler.CompileAssemblyFromFile(cp, sourceFile);
    // Return the results of compilation.
    return cr;
}

[C++] 
static CompilerResults * CompileCode(CodeDomProvider* provider, String* sourceFile, String *exeFile)
{
    // Obtain an ICodeCompiler* from the CodeDomProvider.
    ICodeCompiler* compiler = provider->CreateCompiler();
    // Configure a CompilerParameters that links System.dll
    // and produces the specified executable file.
    String* referenceAssemblies [] = {S"System.dll"};
    CompilerParameters* cp = new CompilerParameters(referenceAssemblies, 
        exeFile, false);
    // Generate an executable rather than a DLL file.
    cp->GenerateExecutable = true;

    // Invoke compilation.
    CompilerResults* cr = compiler->CompileAssemblyFromFile(cp, sourceFile);
    // 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 Language Filter in the upper-left corner of the page.

Requirements

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)

See Also

ICodeCompiler Members | System.CodeDom.Compiler Namespace | CompilerParameters | CompilerResults

Show:
© 2014 Microsoft