AssemblyBuilder Osztály

Definíció

Tárolót biztosít egy szerelvény létrehozásához egy vagy több virtuális útvonalból egy ASP.NET projekten belül.

public ref class AssemblyBuilder
public class AssemblyBuilder
type AssemblyBuilder = class
Public Class AssemblyBuilder
Öröklődés
AssemblyBuilder

Példák

Az alábbi példakód egy egyszerű buildszolgáltatói implementációt mutat be, amely az absztrakt BuildProvider alaposztálytól öröklődik. A buildszolgáltató felülírja az CodeCompilerTypealaposztályt GetGeneratedTypeés GenerateCode a tagokat.

A metódus implementációjában a GenerateCode buildszolgáltató hozzáadja a generált kódot az összeállításhoz a CreateCodeFile metódus használatával. A példa nem tartalmazza az osztály implementálását SampleClassGenerator . További információért lásd 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

Megjegyzések

Az osztálypéldányok AssemblyBuilder osztálymódszerekkel BuildProvider vannak használva egy vagy több fájl lefordított szerelvénybe való összeállításához.

Az BuildProvider osztály az egyes fájlok buildelési funkcióit határozza meg, és az AssemblyBuilder osztály egyesíti az egyes BuildProvider példányok által használt forráskódot egyetlen szerelvényben. A ASP.NET buildkörnyezet egy AssemblyBuilder objektumot ad át a BuildProvider metódusoknak egy vagy több fájlból álló szerelvény létrehozásakor, hogy minden BuildProvider példány hozzá tudja adni a fájl forráskódját a teljes szerelvényhez.

A ASP.NET buildkörnyezet határozza meg a projekten belüli fájlok által igényelt nyelvet és fordítót a BuildProvider.CodeCompilerType tulajdonság alapján. A buildkörnyezet a fordítási beállítások alapján csoportosítja a fájlokat, és szerelvényt készít olyan fájlokból, amelyek ugyanazt a fordítót igénylik.

A CodeDomProvider tulajdonság azt a CodeDomProvider implementációt jelzi, amelyet a ASP.NET buildkörnyezet az egyes BuildProvider implementációk által használt forráskódból való összeállításhoz használ.

Egy BuildProvider objektum codeDOM-gráf formájában járul hozzá a forráskódhoz a AddCodeCompileUnit metódus használatával. Az BuildProvider objektum egy fizikai fájlban tárolt forráskódot ad hozzá a CreateCodeFile metódus használatával.

Miután minden BuildProvider objektum a megfelelő AssemblyBuilder metódusokkal járul hozzá a forráskódhoz, a ASP.NET buildkörnyezet a AssemblyBuilder osztályt használja az összegyűjtött forráskód szerelvénybe való fordításához.

Tulajdonságok

Name Description
CodeDomProvider

Lekéri a forráskód összeállításához használt fordítót egy szerelvénybe.

Metódusok

Name Description
AddAssemblyReference(Assembly)

Egy fájlhoz létrehozott forráskód által hivatkozott szerelvényt ad hozzá.

AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

CodeDOM-gráf formájában adja hozzá a szerelvény forráskódját.

CreateCodeFile(BuildProvider)

Lehetővé teszi a buildszolgáltató számára, hogy ideiglenes forrásfájlt hozzon létre, és a forrásfájlt belefoglalja a szerelvény-összeállításba.

CreateEmbeddedResource(BuildProvider, String)

Lehetővé teszi a buildszolgáltató számára, hogy létrehozzon egy erőforrásfájlt, amely szerepeljen a szerelvény összeállításában.

Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
GenerateTypeFactory(String)

Gyors objektum-előállító sablon beszúrása egy típushoz a lefordított szerelvénybe.

GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetTempFilePhysicalPath(String)

Ideiglenes fájl elérési útját hozza létre.

GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

A következőre érvényes:

Lásd még