AssemblyBuilder AssemblyBuilder AssemblyBuilder AssemblyBuilder Class

Definizione

Rende disponibile un contenitore per compilare un assembly da uno o più percorsi virtuali in un progetto ASP.NET.Provides a container for building an assembly from one or more virtual paths within an ASP.NET project.

public ref class AssemblyBuilder
public class AssemblyBuilder
type AssemblyBuilder = class
Public Class AssemblyBuilder
Ereditarietà
AssemblyBuilderAssemblyBuilderAssemblyBuilderAssemblyBuilder

Esempi

Nell'esempio di codice seguente viene illustrata un'implementazione del provider di compilazione semplice, che BuildProvider eredita dalla classe di base astratta.The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. Il provider di compilazione esegue CodeCompilerTypel' GetGeneratedTypeoverride dei GenerateCode membri, e della classe di base.The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class.

Nell'implementazione del CreateCodeFile metodo,ilproviderdicompilazioneaggiungeilcodicegeneratoperlacompilazionedell'assemblyGenerateCode usando il metodo.In the GenerateCode method implementation, the build provider adds the generated code for the assembly compilation using the CreateCodeFile method. Nell'esempio non è inclusa l'implementazione della SampleClassGenerator classe.The example does not include the implementation of the SampleClassGenerator class. Per ulteriori informazioni, vedere CodeCompileUnit.For more information, see 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

Commenti

Le istanze della AssemblyBuilder classe vengono usate con BuildProvider i metodi della classe per compilare uno o più file in un assembly compilato.Instances of the AssemblyBuilder class are used with BuildProvider class methods to build one or more files into a compiled assembly.

La BuildProvider classe definisce la funzionalità di compilazione per i singoli file AssemblyBuilder e la classe combina il codice sorgente fornito BuildProvider da ogni istanza in un singolo assembly.The BuildProvider class defines build functionality for individual files, and the AssemblyBuilder class combines the source code contributed by each BuildProvider instance into a single assembly. L'ambiente di compilazione ASP.NET passa AssemblyBuilder un oggetto ai BuildProvider metodi durante la compilazione di un assembly da uno o più file, in BuildProvider modo che ogni istanza possa contribuire al codice sorgente per il file nell'assembly globale.The ASP.NET build environment passes an AssemblyBuilder object to the BuildProvider methods when building an assembly from one or more files, so that each BuildProvider instance can contribute source code for its file to the overall assembly.

L'ambiente di compilazione ASP.NET determina il linguaggio e il compilatore necessari per i file all'interno del progetto BuildProvider.CodeCompilerType , in base alla proprietà.The ASP.NET build environment determines the language and compiler required by files within the project, based on the BuildProvider.CodeCompilerType property. L'ambiente di compilazione raggruppa i file in base alle impostazioni del compilatore e compila un assembly dai file che richiedono lo stesso compilatore.The build environment groups files based on their compiler settings and builds an assembly from files that require the same compiler.

La CodeDomProvider proprietà indica l' CodeDomProvider implementazione utilizzata dall'ambiente di compilazione ASP.NET per compilare un assembly dal codice sorgente fornito da ogni BuildProvider implementazione.The CodeDomProvider property indicates the CodeDomProvider implementation that the ASP.NET build environment uses to compile an assembly from the source code contributed by each BuildProvider implementation.

Un BuildProvider oggetto contribuisce al codice sorgente sotto forma di grafo CodeDOM usando il AddCodeCompileUnit metodo.A BuildProvider object contributes source code in the form of a CodeDOM graph using the AddCodeCompileUnit method. Un BuildProvider oggetto contribuisce al codice sorgente archiviato in un file fisico utilizzando CreateCodeFile il metodo.A BuildProvider object contributes source code stored in a physical file using the CreateCodeFile method.

Quando ogni BuildProvider oggetto contribuisce al codice sorgente usando i AssemblyBuilder metodi appropriati, l'ambiente di compilazione ASP.NET AssemblyBuilder usa la classe per compilare il codice sorgente raccolto in un assembly.After each BuildProvider object contributes source code using the appropriate AssemblyBuilder methods, the ASP.NET build environment uses the AssemblyBuilder class to compile the collected source code into an assembly.

Proprietà

CodeDomProvider CodeDomProvider CodeDomProvider CodeDomProvider

Ottiene il compilatore utilizzato per compilare codice sorgente in un assembly.Gets the compiler used to build source code into an assembly.

Metodi

AddAssemblyReference(Assembly) AddAssemblyReference(Assembly) AddAssemblyReference(Assembly) AddAssemblyReference(Assembly)

Aggiunge un assembly a cui fa riferimento il codice sorgente generato per un file.Adds an assembly that is referenced by source code generated for a file.

AddCodeCompileUnit(BuildProvider, CodeCompileUnit) AddCodeCompileUnit(BuildProvider, CodeCompileUnit) AddCodeCompileUnit(BuildProvider, CodeCompileUnit) AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

Aggiunge codice sorgente per l'assembly sotto forma di grafico CodeDOM.Adds source code for the assembly in the form of a CodeDOM graph.

CreateCodeFile(BuildProvider) CreateCodeFile(BuildProvider) CreateCodeFile(BuildProvider) CreateCodeFile(BuildProvider)

Consente a un provider di compilazione di creare un file di origine temporaneo e di includere il file di origine nella compilazione dell'assembly.Allows a build provider to create a temporary source file, and include the source file in the assembly compilation.

CreateEmbeddedResource(BuildProvider, String) CreateEmbeddedResource(BuildProvider, String) CreateEmbeddedResource(BuildProvider, String) CreateEmbeddedResource(BuildProvider, String)

Consente a un provider di compilazione di creare un file di risorse da includere nella compilazione dell'assembly.Allows a build provider to create a resource file to include in the assembly compilation.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateTypeFactory(String) GenerateTypeFactory(String) GenerateTypeFactory(String) GenerateTypeFactory(String)

Inserisce un modello di factory di oggetto veloce per un tipo nell'assembly compilato.Inserts a fast object factory template for a type into the compiled assembly.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetTempFilePhysicalPath(String) GetTempFilePhysicalPath(String) GetTempFilePhysicalPath(String) GetTempFilePhysicalPath(String)

Genera un percorso di file temporaneo.Generates a temporary file path.

GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche