ערוך

שתף באמצעות


BuildProvider Class

Definition

Defines a set of properties and methods for generating source code within the ASP.NET build environment. This class is abstract.

public ref class BuildProvider abstract
public abstract class BuildProvider
type BuildProvider = class
Public MustInherit Class BuildProvider
Inheritance
BuildProvider
Derived

Examples

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. The example does not include the implementation of the SampleClassGenerator class. For more information, see the CodeCompileUnit class overview.

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);
    }
}
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Web.Compilation
Imports System.CodeDom.Compiler
Imports System.CodeDom
Imports System.Security
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Unrestricted := true)> _
Public Class SampleBuildProvider
    Inherits BuildProvider

    Protected _compilerType As CompilerType = Nothing

    Public Sub New()
        _compilerType = GetDefaultCompilerType()
    End Sub

    ' Return the internal CompilerType member 
    ' defined in this implementation.
    Public Overrides ReadOnly Property CodeCompilerType() As CompilerType
        Get
            CodeCompilerType = _compilerType
        End Get
    End Property


    ' Define the build provider implementation of the GenerateCode method.
    Public Overrides Sub GenerateCode(ByVal assemBuilder As AssemblyBuilder)
        ' Generate a code compile unit, and add it to
        ' the assembly builder.

        Dim tw As TextWriter = assemBuilder.CreateCodeFile(Me)
        If Not tw Is Nothing Then
            Try
                ' Generate the code compile unit from the virtual path.
                Dim compileUnit As CodeCompileUnit = _
                        SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath)

                ' Generate the source for the code compile unit, 
                ' and write it to a file specified by the assembly builder.
                Dim provider As CodeDomProvider = assemBuilder.CodeDomProvider
                provider.GenerateCodeFromCompileUnit(compileUnit, tw, Nothing)
            Finally
                tw.Close()
            End Try

        End If
    End Sub

    Public Overrides Function GetGeneratedType(ByVal results As CompilerResults) As System.Type
        Dim typeName As String = SampleClassGenerator.TypeName

        Return results.CompiledAssembly.GetType(typeName)
    End Function

End Class

Remarks

The ASP.NET build environment uses BuildProvider objects to generate source code for different file types within an application. Classes derived from BuildProvider predominantly provide source code for files, Web pages, resources, and other custom items.

Typically, you do not create an instance of the BuildProvider class directly. Instead, you implement a class that derives from BuildProvider, and configure the BuildProvider implementation for use within the ASP.NET build environment.

Instances of the BuildProvider class are used with AssemblyBuilder objects to build one or more files into a compiled assembly. A BuildProvider instance generates source code in the appropriate language for individual files, and the AssemblyBuilder object combines the source contributed by each BuildProvider instance into a single assembly.

The ASP.NET build environment uses instances of the BuildProvider class to build files within an application. The VirtualPath property of the BuildProvider class indicates the path of the file to be built. The file extension of each file within an application is mapped to a corresponding build provider. The ASP.NET build environment initializes a BuildProvider instance for each file based on the file extension, and uses the BuildProvider methods to generate source code for the file. The ASP.NET build environment passes an AssemblyBuilder object based on the preferred compiler language and the context of the file to the BuildProvider methods when building an assembly from one or more files, so that the BuildProvider instance can contribute source code for its file to the overall assembly.

To define custom build actions for a file type within an ASP.NET application, you must derive a class from BuildProvider, implement members within the derived class for building the file type, and configure the build provider for the corresponding file extension within the application configuration file.

The add element specifies the file extension for supported files, and whether the build provider supports code files, Web files, resource files, or all files. Use the type attribute to specify the fully qualified type name of the build provider implementation. Use the BuildProviderAppliesToAttribute class to specify whether the build provider applies to files in the App_Code directory, to files in a Web content directory, to global or local resources, or to all files. Use the extension attribute to specify the file extension used to identify files that the BuildProvider class supports. Use the BuildProviderCollection class to examine build providers in a configuration file. For more information about configuring a build provider, see buildProviders Element for compilation (ASP.NET Settings Schema).

To implement a build provider that generates source code for a custom file type, derive a class from BuildProvider, and override the GenerateCode method to generate source code for the supported file type. The generated source is added to the AssemblyBuilder object in the form of a CodeDOM graph, or as content that represents a physical source code file. If the build provider requires a specific programming language, override the CodeCompilerType property to return a CompilerType object for the supported programming language. If the build provider does not require a specific programming language, do not override the CodeCompilerType property; use the base class implementation, which indicates that the build provider can use any .NET Framework language, such as Visual Basic or C#.

To implement a build provider that generates source code for Web content, derive a class from BuildProvider and override the GetGeneratedType method to return the Type for the class generated by BuildProvider. Override the GenerateCode method to generate source code for the type provided by the supported file.

Note

Adding a customized BuildProvider class to the Web.config file works in an ASP.NET Web site but does not work in an ASP.NET Web application project. In a Web application project, the code that is generated by the BuildProvider class cannot be included in the application. For more information, see ASP.NET Web Application Project Precompilation Overview.

Constructors

BuildProvider()

Initializes a new instance of the BuildProvider class.

Properties

CodeCompilerType

Represents the compiler type used by a build provider to generate source code for a custom file type.

ReferencedAssemblies

Represents the assemblies to compile with the source generated by the build provider.

VirtualPath

Represents the file to be built by this build provider implementation.

VirtualPathDependencies

Represents a collection of virtual paths that must be built before the build provider generates code.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateCode(AssemblyBuilder)

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

GetCodeCompileUnit(IDictionary)

Represents the container for the generated CodeDOM graph.

GetCustomString(CompilerResults)

Generates a string to be persisted in the compiled assembly.

GetDefaultCompilerType()

Returns the compiler settings for the default language in the application.

GetDefaultCompilerTypeForLanguage(String)

Returns the compiler settings for the build provider based on the specified language.

GetGeneratedType(CompilerResults)

Returns a type generated by the build provider from the virtual path.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetResultFlags(CompilerResults)

Returns a value indicating actions required when a virtual path is built.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OpenReader()

Opens a text reader for reading from the virtual path of the current build provider object.

OpenReader(String)

Opens a text reader for reading from a specified virtual path.

OpenStream()

Opens a stream for reading the virtual path of the current build provider object.

OpenStream(String)

Opens a stream for reading from a specified virtual path.

ProcessCompileErrors(CompilerResults)

When overridden in a derived class, enables you to review compiler error messages so that you can modify them to provide more information.

RegisterBuildProvider(String, Type)

Registers a build provider.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also