AssemblyBuilder Klasa

Definicja

Udostępnia kontener do tworzenia zestawu z co najmniej jednej ścieżki wirtualnej w projekcie ASP.NET.

public ref class AssemblyBuilder
public class AssemblyBuilder
type AssemblyBuilder = class
Public Class AssemblyBuilder
Dziedziczenie
AssemblyBuilder

Przykłady

Poniższy przykład kodu ilustruje prostą implementację dostawcy kompilacji dziedziczą po abstrakcyjnej BuildProvider klasie bazowej. Dostawca kompilacji zastępuje CodeCompilerTypeelementy , GetGeneratedTypei GenerateCode elementy członkowskie klasy bazowej.

W implementacji GenerateCode metody dostawca kompilacji dodaje wygenerowany kod kompilacji zestawu przy użyciu CreateCodeFile metody . Przykład nie zawiera implementacji SampleClassGenerator klasy . Aby uzyskać więcej informacji, zobacz CodeCompileUnit.

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()
    {
        // Set the compiler to use Visual Basic.
        _compilerType = GetDefaultCompilerTypeForLanguage("C#");
    }

    // Return the internal CompilerType member 
    // defined in this implementation.
    public override CompilerType CodeCompilerType
    {
        get { return _compilerType; }
    }

    // Define a method that returns details for the 
    // code compiler for this build provider.
    public string GetCompilerTypeDetails()
    {
        StringBuilder details = new StringBuilder("");

        if (_compilerType != null)
        {
            // Format a string that contains the code compiler
            // implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; \n",
                _compilerType.CodeDomProviderType.ToString());
            details.AppendFormat("Compiler debug build = {0}; \n",
                _compilerType.CompilerParameters.IncludeDebugInformation.ToString());
            details.AppendFormat("Compiler warning level = {0}; \n",
                _compilerType.CompilerParameters.WarningLevel.ToString());

            if (_compilerType.CompilerParameters.CompilerOptions != null)
            {
                details.AppendFormat("Compiler options: {0}; \n",
                    _compilerType.CompilerParameters.CompilerOptions.ToString());
            }
        }
        return details.ToString();
    }

    // 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.CreateGenerator().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 a method that returns details for the 
    ' code compiler for this build provider.
    Public Function GetCompilerTypeDetails() As String
        Dim details As StringBuilder = New StringBuilder("")

        If Not _compilerType Is Nothing Then
            ' Format a string that contains the code compiler
            ' implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; ", _
                _compilerType.CodeDomProviderType.ToString())
            details.AppendFormat("Compiler debug build = {0}; ", _
                _compilerType.CompilerParameters.IncludeDebugInformation.ToString())
            details.AppendFormat("Compiler warning level = {0}; ", _
                _compilerType.CompilerParameters.WarningLevel.ToString())

            If Not _compilerType.CompilerParameters.CompilerOptions Is Nothing Then
                details.AppendFormat("Compiler options: {0}; ", _
                    _compilerType.CompilerParameters.CompilerOptions.ToString())
            End If
        End If
        Return details.ToString()
    End Function

    ' 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.CreateGenerator().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

Uwagi

AssemblyBuilder Wystąpienia klasy są używane z metodami BuildProvider klasy do kompilowania co najmniej jednego pliku w skompilowanym zestawie.

Klasa BuildProvider definiuje funkcje kompilacji dla poszczególnych plików, a AssemblyBuilder klasa łączy kod źródłowy współautora każdego BuildProvider wystąpienia w jeden zestaw. Środowisko kompilacji ASP.NET przekazuje AssemblyBuilder obiekt do BuildProvider metod podczas kompilowania zestawu z co najmniej jednego pliku, dzięki czemu każde BuildProvider wystąpienie może współtworzyć kod źródłowy dla jego pliku do ogólnego zestawu.

Środowisko kompilacji ASP.NET określa język i kompilator wymagany przez pliki w projekcie na BuildProvider.CodeCompilerType podstawie właściwości . Środowisko kompilacji grupuje pliki na podstawie ustawień kompilatora i tworzy zestaw z plików, które wymagają tego samego kompilatora.

Właściwość CodeDomProvider wskazuje implementację CodeDomProvider , która jest używana przez środowisko kompilacji ASP.NET do kompilowania zestawu z kodu źródłowego przez każdą BuildProvider implementację.

Obiekt BuildProvider współtworzy kod źródłowy w postaci grafu CodeDOM przy użyciu AddCodeCompileUnit metody . Obiekt BuildProvider współtworzy kod źródłowy przechowywany w pliku fizycznym przy użyciu CreateCodeFile metody .

Gdy każdy BuildProvider obiekt współtworzy kod źródłowy przy użyciu odpowiednich AssemblyBuilder metod, środowisko kompilacji ASP.NET używa AssemblyBuilder klasy do kompilowania zebranego kodu źródłowego do zestawu.

Właściwości

CodeDomProvider

Pobiera kompilator używany do kompilowania kodu źródłowego w zestawie.

Metody

AddAssemblyReference(Assembly)

Dodaje zestaw, do którego odwołuje się kod źródłowy wygenerowany dla pliku.

AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

Dodaje kod źródłowy zestawu w postaci grafu CodeDOM.

CreateCodeFile(BuildProvider)

Umożliwia dostawcy kompilacji utworzenie tymczasowego pliku źródłowego i dołączenie pliku źródłowego do kompilacji zestawu.

CreateEmbeddedResource(BuildProvider, String)

Umożliwia dostawcy kompilacji utworzenie pliku zasobu do uwzględnienia w kompilacji zestawu.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GenerateTypeFactory(String)

Wstawia szablon szybkiej fabryki obiektów dla typu do skompilowanego zestawu.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetTempFilePhysicalPath(String)

Generuje tymczasową ścieżkę pliku.

GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też