共用方式為


BuildProvider.GenerateCode(AssemblyBuilder) 方法

定義

產生組建提供者虛擬路徑的原始程式碼,並將原始程式碼加入到指定的組件產生器。

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
Public Overridable Sub GenerateCode (assemblyBuilder As AssemblyBuilder)

參數

assemblyBuilder
AssemblyBuilder

參考組建提供者所產生之原始程式碼的組件產生器。

範例

下列程式碼範例說明繼承自抽象 BuildProvider 基類的簡單組建提供者實作。 組建提供者會 CodeCompilerType 覆寫基類的 、 GetGeneratedTypeGenerateCode 成員。

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

備註

若要實作產生自訂檔案類型原始程式碼的組建提供者,請從 BuildProvider 類別衍生類別,並覆寫 GenerateCode 方法以產生支援的檔案類型的原始程式碼。

一般而言,組建提供者的實作 GenerateCode 會讀取 VirtualPath 屬性、剖析內容,並將產生的原始程式碼新增至指定的 AssemblyBuilder 物件。 您可以以 物件或 CodeDOM 圖形的形式 TextWriter ,將原始程式碼提供給元件。 若要透過 TextWriter 物件將原始程式碼新增至元件,請使用 CreateCodeFile 方法。 若要將 CodeDOM 圖形中的原始程式碼新增至元件,請使用 AddCodeCompileUnit 方法。

如果組建提供者在特定程式設計語言中產生原始程式碼,您必須覆寫 CodeCompilerType 屬性,以傳回 CompilerType 支援程式設計語言的物件。

適用於

另請參閱