Export (0) Print
Expand All

AssemblyBuilder.CreateCodeFile Method

Allows a build provider to create a temporary source file, and include the source file in the assembly compilation.

Namespace:  System.Web.Compilation
Assembly:  System.Web (in System.Web.dll)

public TextWriter CreateCodeFile(
	BuildProvider buildProvider


Type: System.Web.Compilation.BuildProvider

The build provider generating the code source file.

Return Value

Type: System.IO.TextWriter
An open TextWriter that can be used to write source code to a temporary file.

A BuildProvider implementation calls the CreateCodeFile method when generating a source file for a virtual path. Source code added with CreateCodeFile is included in the assembly compilation.

Typically, a build provider GenerateCode method implementation reads the VirtualPath property, parses the contents, and then adds the generated source code to the specified AssemblyBuilder object. The build provider uses the CreateCodeFile method to add source code as a physical file to the assembly. Alternately, the build provider can use the AddCodeCompileUnit method to add source code as a CodeDOM graph to the assembly.

After calling CreateCodeFile, the build provider writes the source file contents using the returned TextWriter object. After writing the source file, the BuildProvider object must use the Close method to close the TextWriter object and free associated system resources.

The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class.

In the GenerateCode method implementation, the build provider adds the generated code for the assembly compilation using the CreateCodeFile method. The example does not include the implementation of the SampleClassGenerator class. For more information, see CodeCompileUnit.

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Security;
using System.Security.Permissions;

// Define a simple build provider implementation.
[PermissionSet(SecurityAction.Demand, Unrestricted = true)]
public class SampleBuildProvider : BuildProvider
    // Define an internal member for the compiler type. 
    protected CompilerType _compilerType = null;

    public SampleBuildProvider()
		// Set the compiler to use Visual Basic.
		_compilerType = GetDefaultCompilerTypeForLanguage("C#");

    // Return the internal CompilerType member  
    // defined in this implementation. 
    public override CompilerType CodeCompilerType
        get { return _compilerType; }

    // Define a method that returns details for the  
    // code compiler for this build provider. 
    public string GetCompilerTypeDetails()
        StringBuilder details = new StringBuilder("");

        if (_compilerType != null)
            // Format a string that contains the code compiler 
            // implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; \n",
            details.AppendFormat("Compiler debug build = {0}; \n",
            details.AppendFormat("Compiler warning level = {0}; \n",

            if (_compilerType.CompilerParameters.CompilerOptions != null)
                details.AppendFormat("Compiler options: {0}; \n",
        return details.ToString();

    // Define the build provider implementation of the GenerateCode method. 
    public override void GenerateCode(AssemblyBuilder assemBuilder)
        // Generate a code compile unit, and add it to 
        // the assembly builder.

        TextWriter tw = assemBuilder.CreateCodeFile(this);
        if (tw != null)
                // Generate the code compile unit from the virtual path.
                CodeCompileUnit compileUnit = SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath);

                // Generate the source for the code compile unit,  
                // and write it to a file specified by the assembly builder.
                CodeDomProvider provider = assemBuilder.CodeDomProvider;
                provider.CreateGenerator().GenerateCodeFromCompileUnit(compileUnit, tw, null);

    public override System.Type GetGeneratedType(CompilerResults results)
        string typeName = SampleClassGenerator.TypeName;

        return results.CompiledAssembly.GetType(typeName);

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft