Bagikan melalui


AssemblyBuilder Kelas

Definisi

Menyediakan kontainer untuk membangun rakitan dari satu atau beberapa jalur virtual dalam proyek ASP.NET.

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

Contoh

Contoh kode berikut mengilustrasikan implementasi penyedia build sederhana, mewarisi dari kelas dasar abstrak BuildProvider . Penyedia build mengambil alih CodeCompilerType, , GetGeneratedTypedan GenerateCode anggota kelas dasar.

GenerateCode Dalam implementasi metode, penyedia build menambahkan kode yang dihasilkan untuk kompilasi perakitan menggunakan CreateCodeFile metode . Contoh tidak termasuk implementasi SampleClassGenerator kelas. Untuk informasi selengkapnya, lihat 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

Keterangan

Instans AssemblyBuilder kelas digunakan dengan BuildProvider metode kelas untuk membangun satu atau beberapa file ke dalam rakitan yang dikompilasi.

Kelas BuildProvider menentukan fungsionalitas build untuk file individual, dan AssemblyBuilder kelas menggabungkan kode sumber yang dikontribusikan oleh setiap BuildProvider instans ke dalam satu rakitan. Lingkungan build ASP.NET meneruskan AssemblyBuilder objek ke BuildProvider metode saat membangun perakitan dari satu atau beberapa file, sehingga setiap BuildProvider instans dapat menyumbangkan kode sumber untuk filenya ke perakitan keseluruhan.

Lingkungan build ASP.NET menentukan bahasa dan pengkompilasi yang diperlukan oleh file dalam proyek, berdasarkan BuildProvider.CodeCompilerType properti . Lingkungan build mengelompokkan file berdasarkan pengaturan pengkompilasinya dan membangun perakitan dari file yang memerlukan pengkompilasi yang sama.

Properti CodeDomProvider menunjukkan CodeDomProvider implementasi yang digunakan lingkungan build ASP.NET untuk mengkompilasi rakitan dari kode sumber yang dikontribusikan oleh setiap BuildProvider implementasi.

Objek BuildProvider berkontribusi kode sumber dalam bentuk grafik CodeDOM menggunakan metode .AddCodeCompileUnit Objek BuildProvider berkontribusi kode sumber yang disimpan dalam file fisik menggunakan metode .CreateCodeFile

Setelah setiap BuildProvider objek berkontribusi kode sumber menggunakan metode yang sesuai AssemblyBuilder , lingkungan build ASP.NET menggunakan AssemblyBuilder kelas untuk mengkompilasi kode sumber yang dikumpulkan ke dalam rakitan.

Properti

Nama Deskripsi
CodeDomProvider

Mendapatkan pengkompilasi yang digunakan untuk membangun kode sumber ke dalam rakitan.

Metode

Nama Deskripsi
AddAssemblyReference(Assembly)

Menambahkan rakitan yang dirujuk oleh kode sumber yang dihasilkan untuk file.

AddCodeCompileUnit(BuildProvider, CodeCompileUnit)

Menambahkan kode sumber untuk rakitan dalam bentuk grafik CodeDOM.

CreateCodeFile(BuildProvider)

Memungkinkan penyedia build untuk membuat file sumber sementara, dan menyertakan file sumber dalam kompilasi perakitan.

CreateEmbeddedResource(BuildProvider, String)

Memungkinkan penyedia build untuk membuat file sumber daya untuk disertakan dalam kompilasi perakitan.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GenerateTypeFactory(String)

Menyisipkan templat pabrik objek cepat untuk jenis ke dalam rakitan yang dikompilasi.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetTempFilePhysicalPath(String)

Menghasilkan jalur file sementara.

GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Lihat juga