Freigeben über


BuildProvider.GenerateCode(AssemblyBuilder) Methode

Definition

Generiert Quellcode für den virtuellen Pfad des Buildanbieters und fügt den Quellcode einem angegebenen Assembly-Generator hinzu.

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)

Parameter

assemblyBuilder
AssemblyBuilder

Der Assembly-Generator, der auf den vom Buildanbieter generierten Quellcode verweist.

Beispiele

Das folgende Codebeispiel veranschaulicht eine einfache Buildanbieterimplementierung, die von der abstrakten BuildProvider Basisklasse erbt. Der Buildanbieter überschreibt die CodeCompilerType, GetGeneratedTypeund GenerateCode Member der Basisklasse.

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

Hinweise

Um einen Buildanbieter zu implementieren, der Quellcode für einen benutzerdefinierten Dateityp generiert, leiten Sie eine Klasse von der BuildProvider Klasse ab, und überschreiben Sie die GenerateCode Methode, um Quellcode für den unterstützten Dateityp zu generieren.

In der Regel liest die Implementierung eines Buildanbieters GenerateCode die VirtualPath Eigenschaft, analysiert den Inhalt und fügt dem angegebenen AssemblyBuilder Objekt den generierten Quellcode hinzu. Sie können Quellcode zur Assembly in Form eines TextWriter Objekts oder eines CodeDOM-Diagramms beitragen. Verwenden Sie die CreateCodeFile Methode, um der Assembly über ein TextWriter Objekt Quellcode hinzuzufügen. Wenn Sie der Assembly Quellcode in einem CodeDOM-Diagramm hinzufügen möchten, verwenden Sie die AddCodeCompileUnit Methode.

Wenn der Buildanbieter Quellcode in einer bestimmten Programmiersprache generiert, müssen Sie die CodeCompilerType Eigenschaft überschreiben, um ein CompilerType Objekt für die unterstützte Programmiersprache zurückzugeben.

Gilt für:

Weitere Informationen