.NET Framework Class Library  

CompilerParameters Class

Represents the parameters used to invoke a compiler.

For a list of all members of this type, see CompilerParameters Members.

System.Object
   System.CodeDom.Compiler.CompilerParameters

[Visual Basic]
<ComVisible(False)>
Public Class CompilerParameters
[C#]
[ComVisible(false)]
public class CompilerParameters
[C++]
[ComVisible(false)]
public __gc class CompilerParameters
[JScript]
public
   ComVisible(false)
class CompilerParameters

Thread Safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Remarks

A CompilerParameters object represents the settings and options for an ICodeCompiler interface.

If you are compiling an executable program, you must set the GenerateExecutable property to true. When the GenerateExecutable is set to false, the compiler will generate a class library. By default, a new CompilerParameters is initialized with its GenerateExecutable property set to false. If you are compiling an executable from a CodeDOM graph, a CodeEntryPointMethod must be defined in the graph. If there are multiple code entry points, you can indicate the class that defines the entry point to use by setting the name of the class to the MainClass property.

You can specify a file name for the output assembly in the OutputAssembly property. Otherwise, a default output file name will be used. To include debug information in a generated assembly, set the IncludeDebugInformation property to true. If your project references any assemblies, you must specify the assembly names as items in a StringCollection set to the ReferencedAssemblies property of the CompilerParameters used when invoking compilation.

You can compile an assembly that is written to memory rather than disk by setting the GenerateInMemory property to true. When an assembly is generated in memory, your code can obtain a reference to the generated assembly from the CompiledAssembly property of a CompilerResults. If an assembly is written to disk, you can obtain the path to the generated assembly from the PathToAssembly property of a CompilerResults.

To specify a warning level at which to halt compilation, set the WarningLevel property to an integer that represents the warning level at which to halt compilation. You can also configure the compiler to halt compilation if warnings are encountered by setting the TreatWarningsAsErrors property to true.

To specify a custom command-line arguments string to use when invoking the compilation process, set the string in the CompilerOptions property. If a Win32 security token is required to invoke the compiler process, specify the token in the UserToken property. To link a Win32 resource file into the compiled assembly, specify the name of the Win32 resource file in the Win32Resource property.

Note   When building an executable, be sure to set the GenerateExecutable property to true. The default value of this property is false, which causes the compiler to generate a class library.

Example

[Visual Basic] 
Public Function CreateCompilerParameters() As CompilerParameters
    ' Declares a CompilerParameters.
    Dim cp As New CompilerParameters()

    ' Generates an executable instead of 
    ' a class library.
    cp.GenerateExecutable = True

    ' Specifies the class containing the
    ' CodeEntryPointMethod to use to start the 
    ' process.  This is uesful when a project
    ' contains multiple CodeEntryPointMethods.
    cp.MainClass = "Samples.Class1"

    ' Sets filename of the assembly file to generate.
    cp.OutputAssembly = "output.exe"

    ' Generates debug information.
    cp.IncludeDebugInformation = True

    ' Adds an assembly reference.
    cp.ReferencedAssemblies.Add("System.dll")

    ' Sets whether to generate the assembly in memory.
    cp.GenerateInMemory = False

    ' Sets the warning level at which 
    ' the compiler should abort compilation
    ' if a warning of this level occurrs.
    cp.WarningLevel = 3

    ' Sets whether to treat all warnings as errors.
    cp.TreatWarningsAsErrors = False

    ' Sets a command line compiler arguments string.
    cp.CompilerOptions = "/optimize"

    ' Sets a temporary files collection.
    ' This TempFileCollection stores the temporary files
    ' generated during a build in the current directory,
    ' and does not delete them after compilation.
    cp.TempFiles = New TempFileCollection("..", True)

    Return cp
End Function

[C#] 
public CompilerParameters CreateCompilerParameters()
{
    // Declares a CompilerParameters.
    CompilerParameters cp = new CompilerParameters();

    // Generates an executable instead of 
    // a class library.
    cp.GenerateExecutable = true;

    // Specifies the class containing the
    // CodeEntryPointMethod to use to start the 
    // process.  This is uesful when a project
    // contains multiple CodeEntryPointMethods.
    cp.MainClass = "Samples.Class1";

    // Sets filename of the assembly file to generate.
    cp.OutputAssembly = "output.exe";

    // Generates debug information.
    cp.IncludeDebugInformation = true;

    // Adds an assembly reference.
    cp.ReferencedAssemblies.Add( "System.dll" );

    // Sets whether to generate the assembly in memory.
    cp.GenerateInMemory = false;

    // Sets the warning level at which 
    // the compiler should abort compilation
    // if a warning of this level occurrs.
    cp.WarningLevel = 3;

    // Sets whether to treat all warnings as errors.
    cp.TreatWarningsAsErrors = false;
    
    // Sets a command line compiler arguments string.
    cp.CompilerOptions = "/optimize";

     // Sets a temporary files collection.
    // This TempFileCollection stores the temporary files
    // generated during a build in the current directory,
    // and does not delete them after compilation.
    cp.TempFiles = new TempFileCollection("..", true);

    return cp;
}

[C++] 
public:
   CompilerParameters * CreateCompilerParameters() {
      // Declares a CompilerParameters.
      CompilerParameters* cp = new CompilerParameters();

      // Generates an executable instead of
      // a class library.
      cp->GenerateExecutable = true;

      // Specifies the class containing the
      // CodeEntryPointMethod to use to start the
      // process.  This is uesful when a project
      // contains multiple CodeEntryPointMethods.
      cp->MainClass = S"Samples::Class1";

      // Sets filename of the assembly file to generate.
      cp->OutputAssembly = S"output.exe";

      // Generates debug information.
      cp->IncludeDebugInformation = true;

      // Adds an assembly reference.
      cp->ReferencedAssemblies->Add(S"System.dll");

      // Sets whether to generate the assembly in memory.
      cp->GenerateInMemory = false;

      // Sets the warning level at which
      // the compiler should abort compilation
      // if a warning of this level occurrs.
      cp->WarningLevel = 3;

      // Sets whether to treat all warnings as errors.
      cp->TreatWarningsAsErrors = false;

      // Sets a command line compiler arguments String*.
      cp->CompilerOptions = S"/optimize";

      // Sets a temporary files collection.
      // This TempFileCollection stores the temporary files
      // generated during a build in the current directory,
      // and does not delete them after compilation.
      cp->TempFiles = new TempFileCollection(S"..", true);

      return cp;
   }

[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

CompilerParameters Members | System.CodeDom.Compiler Namespace