BuildProvider.GenerateCode(AssemblyBuilder) BuildProvider.GenerateCode(AssemblyBuilder) BuildProvider.GenerateCode(AssemblyBuilder) Method

定义

为生成提供程序的虚拟路径生成源代码,并将源代码添加到指定程序集生成器。 Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.

public:
 virtual void GenerateCode(System::Web::Compilation::AssemblyBuilder ^ assemblyBuilder);
public virtual void GenerateCode (System.Web.Compilation.AssemblyBuilder assemblyBuilder);
abstract member GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
override this.GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
参数
assemblyBuilder
AssemblyBuilder AssemblyBuilder AssemblyBuilder

引用生成提供程序所生成的源代码的程序集生成器。 The assembly builder that references the source code generated by the build provider.

示例

下面的代码示例演示了一个简单的生成提供程序实现,继承自抽象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.

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 Microsoft.VisualBasic
Imports System
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

注解

若要实现为自定义文件类型生成源代码的生成提供程序,从派生类BuildProvider类,并重写GenerateCode方法生成的源代码的受支持的文件类型。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.

通常情况下,生成提供程序的GenerateCode实现会读取VirtualPath属性,分析内容,并将生成的源代码添加到指定AssemblyBuilder对象。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. 你可以参与的窗体中的程序集源代码TextWriter对象或 CodeDOM 图。You can contribute source code to the assembly in the form of a TextWriter object or a CodeDOM graph. 若要将源代码添加到通过程序集TextWriter对象,请使用CreateCodeFile方法。To add source code to the assembly through a TextWriter object, use the CreateCodeFile method. 若要对程序集在 CodeDOM 图添加源代码,请使用AddCodeCompileUnit方法。To add source code in a CodeDOM graph to the assembly, use the AddCodeCompileUnit method.

如果生成提供程序特定的编程语言生成源代码,则必须重写CodeCompilerType属性以返回CompilerType的受支持的编程语言的对象。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.

适用于

另请参阅