BuildProvider 类

定义

定义用于在 ASP.NET 生成环境中生成源代码的一组属性和方法。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
继承
BuildProvider
派生

示例

下面的代码示例演示了一个简单的生成提供程序实现,该BuildProvider实现继承自抽象基类。The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. 生成提供程序会重CodeCompilerTypeGetGeneratedType基类的GenerateCode 、和成员。The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class. 该示例不包括SampleClassGenerator类的实现。The example does not include the implementation of the SampleClassGenerator class. 有关详细信息,请参阅CodeCompileUnit类概述。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

注解

ASP.NET 生成环境使用BuildProvider对象为应用程序中的不同文件类型生成源代码。The ASP.NET build environment uses BuildProvider objects to generate source code for different file types within an application. 从主要派生BuildProvider的类提供文件、网页、资源和其他自定义项的源代码。Classes derived from BuildProvider predominantly provide source code for files, Web pages, resources, and other custom items.

通常不会直接创建BuildProvider类的实例。Typically, you do not create an instance of the BuildProvider class directly. 相反,您实现了一个派生自BuildProvider的类,并配置了BuildProvider实现以便在 ASP.NET 生成环境中使用。Instead, you implement a class that derives from BuildProvider, and configure the BuildProvider implementation for use within the ASP.NET build environment.

类的实例与AssemblyBuilder对象一起使用,以将一个或多个文件生成到已编译的程序集中。 BuildProviderInstances of the BuildProvider class are used with AssemblyBuilder objects to build one or more files into a compiled assembly. 实例以适当的语言为各个文件生成源代码, AssemblyBuilder并且对象将每个BuildProvider实例提供的源组合到单个程序集中。 BuildProviderA 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.

ASP.NET 生成环境使用BuildProvider类的实例在应用程序内生成文件。The ASP.NET build environment uses instances of the BuildProvider class to build files within an application. BuildProviderVirtualPath的属性指示要生成的文件的路径。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. ASP.NET 生成环境根据文件扩展名BuildProvider为每个文件初始化一个实例,并BuildProvider使用方法来生成该文件的源代码。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. 当从一个或多个AssemblyBuilder BuildProvider文件生成程序集时,ASP.NET 生成环境会根据首选编译器语言和BuildProvider文件上下文将对象传递给方法,以便实例可以参与整个程序集的文件的源代码。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.

若要为 ASP.NET 应用程序中的文件类型定义自定义生成操作,你必须从BuildProvider派生一个类,在派生类中实现成员以生成文件类型,并为相应的文件扩展名配置生成提供程序在应用程序配置文件中。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.

指定受支持文件的文件扩展名,以及生成提供程序是否支持代码文件、Web 文件、资源文件或所有文件。The specifies the file extension for supported files, and whether the build provider supports code files, Web files, resource files or all files. type使用特性指定生成提供程序实现的完全限定类型名称。Use the type attribute to specify the fully qualified type name of the build provider implementation. BuildProviderAppliesToAttribute使用类可指定生成提供程序是应用于 App_Code 目录中的文件、Web 内容目录中的文件、全局还是本地资源,还是应用于所有文件。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. 使用特性可指定用于标识BuildProvider类支持的文件的文件扩展名。 extensionUse the extension attribute to specify the file extension used to identify files that the BuildProvider class supports. BuildProviderCollection使用类检查配置文件中的生成提供程序。Use the BuildProviderCollection class to examine build providers in a configuration file. 有关配置生成提供程序的详细信息,请参阅用于编译的 BuildProviders 元素(ASP.NET 设置架构)For more information about configuring a build provider, see buildProviders Element for compilation (ASP.NET Settings Schema).

若要实现生成提供程序,以便为自定义文件类型生成源代码,请从BuildProvider派生一个类,并重GenerateCode写方法以为支持的文件类型生成源代码。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. 生成的源以 CodeDOM 图的AssemblyBuilder形式添加到对象中,或以表示物理源代码文件的内容的形式添加。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. 如果生成提供程序需要特定的编程语言,则重CodeCompilerType写属性以CompilerType返回受支持的编程语言的对象。If the build provider requires a specific programming language, override the CodeCompilerType property to return a CompilerType object for the supported programming language. 如果生成提供程序不需要特定编程语言,则不要重写CodeCompilerType属性; 使用基类实现,这表示生成提供程序可以使用任何 .NET Framework 语言,如 Visual Basic 或。 C#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#.

若要实现生成 Web 内容源代码的生成提供程序BuildProvider ,请从派生一个类,并重GetGeneratedType写方法以返回由Type BuildProvider生成的类的。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. 重写GenerateCode方法以生成受支持文件提供的类型的源代码。Override the GenerateCode method to generate source code for the type provided by the supported file.

备注

向 web.config 文件BuildProvider中添加自定义类在 ASP.NET 网站中运行,但不能在 ASP.NET web 应用程序项目中使用。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. 在 Web 应用程序项目中,不能在应用程序中BuildProvider包含由类生成的代码。In a Web application project, the code that is generated by the BuildProvider class cannot be included in the application. 有关详细信息,请参阅ASP.NET Web 应用程序项目预编译概述For more information, see ASP.NET Web Application Project Precompilation Overview.

构造函数

BuildProvider()

初始化 BuildProvider 类的新实例。Initializes a new instance of the BuildProvider class.

属性

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.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 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)

表示生成的 CodeDOM 关系图的容器。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.

(继承自 Object)
GetResultFlags(CompilerResults)

返回指示生成虚拟路径时所需操作的值。Returns a value indicating actions required when a virtual path is built.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 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.

(继承自 Object)

适用于

另请参阅