Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CompilerParameters Class

Represents the parameters used to invoke a compiler.

Namespace: System.CodeDom.Compiler
Assembly: System (in system.dll)

[SerializableAttribute] 
public class CompilerParameters
/** @attribute SerializableAttribute() */ 
public class CompilerParameters
SerializableAttribute 
public class CompilerParameters
Not applicable.

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 include .NET Framework resource files in the compiled assembly, add the names of the resource files to the EmbeddedResources property. To reference .NET Framework resources in another assembly, add the names of the resource files to the LinkedResources property. To include a Win32 resource file in the compiled assembly, specify the name of the Win32 resource file in the Win32Resource property.

NoteNote:

This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands.

The following example illustrates using CompilerParameters to specify various compiler settings and options.

public static bool CompileCode(CodeDomProvider provider, 
    String sourceFile, 
    String exeFile)
{

    CompilerParameters cp = new CompilerParameters();

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

    // Set the assembly file name to generate.
    cp.OutputAssembly = exeFile;

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

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

    // Save the assembly as a physical file.
    cp.GenerateInMemory = false;

    // Set the level at which the compiler 
    // should start displaying warnings.
    cp.WarningLevel = 3;

    // Set whether to treat all warnings as errors.
    cp.TreatWarningsAsErrors = false;
    
    // Set compiler argument to optimize output.
    cp.CompilerOptions = "/optimize";

    // Set a temporary files collection.
    // The 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);

    if (provider.Supports(GeneratorSupport.EntryPointMethod))
    {
        // Specify the class that contains 
        // the main method of the executable.
        cp.MainClass = "Samples.Class1";
    }
  
    if (provider.Supports(GeneratorSupport.Resources))
    {
        // Set the embedded resource file of the assembly.
        // This is useful for culture-neutral resources, 
        // or default (fallback) resources.
        cp.EmbeddedResources.Add("Default.resources");

        // Set the linked resource reference files of the assembly.
        // These resources are included in separate assembly files,
        // typically localized for a specific language and culture.
        cp.LinkedResources.Add("nb-no.resources");
    }

    // Invoke compilation.
    CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);

    if(cr.Errors.Count > 0)
    {
        // Display compilation errors.
        Console.WriteLine("Errors building {0} into {1}",  
            sourceFile, cr.PathToAssembly);
        foreach(CompilerError ce in cr.Errors)
        {
            Console.WriteLine("  {0}", ce.ToString());
            Console.WriteLine();
        }
    }
    else
    {
        Console.WriteLine("Source {0} built into {1} successfully.",
            sourceFile, cr.PathToAssembly);
        Console.WriteLine("{0} temporary files created during the compilation.",
            cp.TempFiles.Count.ToString());

    }
  
    // Return the results of compilation.
    if (cr.Errors.Count > 0)
    {
        return false;
    }
    else 
    {
        return true;
    }
}

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.