BuildProvider.GenerateCode Method

Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.

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

public virtual void GenerateCode(
	AssemblyBuilder assemblyBuilder
)

Parameters

assemblyBuilder
Type: System.Web.Compilation.AssemblyBuilder

The assembly builder that references the source code generated by the build provider.

To implement a build provider that generates source code for a custom file type, derive a class from the BuildProvider class, and override the GenerateCode method to generate source code for the supported file type.

Typically, a build provider's GenerateCode implementation reads the VirtualPath property, parses the contents, and adds the generated source code to the specified AssemblyBuilder object. You can contribute source code to the assembly in the form of a TextWriter object or a CodeDOM graph. To add source code to the assembly through a TextWriter object, use the CreateCodeFile method. To add source code in a CodeDOM graph to the assembly, use the AddCodeCompileUnit method.

If the build provider generates source code in a specific programming language, you must override the CodeCompilerType property to return a CompilerType object for the supported programming language.

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.

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()
    {
        _compilerType = GetDefaultCompilerTypeForLanguage("C#");
    }

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


    // 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)
        {
            try
            {
                // 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.GenerateCodeFromCompileUnit(compileUnit, tw, null);
            }
            finally
            {
                tw.Close();
            }
        }
    }

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

        return results.CompiledAssembly.GetType(typeName);
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft