IAssemblyPostProcessor 介面


定義類別實作的方法,用來在已建置組件後處理組件。Defines the method a class implements to process an assembly after the assembly has been built.

public interface class IAssemblyPostProcessor : IDisposable
public interface IAssemblyPostProcessor : IDisposable
type IAssemblyPostProcessor = interface
    interface IDisposable
Public Interface IAssemblyPostProcessor
Implements IDisposable


下列程式碼範例示範如何建立IAssemblyPostProcessor介面的執行, 並在 web 應用程式的 web.config 檔案中註冊它。The following code example demonstrates how to create an implementation of the IAssemblyPostProcessor interface, and register it in the Web.config file of a Web application.

程式碼範例的第一個部分會建立一個名Samples.Process.postProcessTest為的類別IAssemblyPostProcessor , 它會執行介面。The first part of the code example creates a class named Samples.Process.postProcessTest that implements the IAssemblyPostProcessor interface. 呼叫PostProcessAssembly方法時, 這個類別會執行寫入檔案的簡單動作。This class performs the simple action of writing a file when the PostProcessAssembly method is called.

using System;
using System.Web.Compilation;
using System.IO;

namespace Samples.Process
    public class postProcessTest : IAssemblyPostProcessor
        public static void Main(String[] args)

        public void PostProcessAssembly(string path)
            StreamWriter sw = File.CreateText(@"c:\compile\MyTest.txt");
            sw.WriteLine("Compiled assembly:");

        public void Dispose()

Imports System.Web.Compilation
Imports System.IO

Namespace Samples.Process
    Public Class postProcessTest
        Implements IAssemblyPostProcessor

        Sub Main()

        End Sub

        Public Sub PostProcessAssembly(ByVal path As String) _
            Implements IAssemblyPostProcessor.PostProcessAssembly
            Dim sw As StreamWriter
            sw = File.CreateText("c:\compile\MyTest.txt")
            sw.WriteLine("Compiled assembly:")
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose

        End Sub
    End Class
End Namespace

使用命令csc /target:library postProcessTest.cs, 將類別編譯成 .dll 檔案。Compile the class into a .dll file with the command csc /target:library postProcessTest.cs. 將產生的 .dll 檔案新增至 ASP.NET 應用程式的 Bin 資料夾, 並在 web.config 檔案中註冊 .dll, 如下列程式碼所示。Add the resulting .dll file to the Bin folder of an ASP.NET application and register the .dll in the Web.config file, as shown in the following code.

<compilation debug="true" assemblyPostProcessorType="Samples.Process.postProcessTest" />  

當使用者造訪網站時, 會以動態方式編譯 Web 應用程式, 並將 MyTest 檔案寫入 C:\compile。When a user visits the Web site, the Web application is dynamically compiled and the file MyTest.txt will be written to C:\compile.


執行此介面的類別可以在編譯之後存取元件。A class implementing this interface can access an assembly after it has been compiled. 類別會編譯元件, 然後檢查IAssemblyPostProcessor是否已在 Web 設定檔中註冊介面。 AssemblyBuilderThe AssemblyBuilder class compiles assemblies and then checks to see whether an IAssemblyPostProcessor interface has been registered in the Web configuration file. 若是如此, AssemblyBuilder實例會針對IAssemblyPostProcessor介面PostProcessAssembly呼叫方法, 以便在編譯之後和載入元件之前執行任何動作。If so, the AssemblyBuilder instance calls the PostProcessAssembly method for the IAssemblyPostProcessor interface to perform any action after the compilation and before loading the assembly. 例如, 分析工具可以執行此介面來建立元件中的探查。For example, a profiler tool could implement this interface to establish probes in the assembly.

IAssemblyPostProcessor註冊介面時, ASP.NET 應用程式及其元件一律會在「偵測模式」中進行編譯。When an IAssemblyPostProcessor interface is registered, the ASP.NET application and its assemblies will always be compiled in debug mode.



執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(繼承來源 IDisposable)

在載入組件之前呼叫,以允許實作的類別修改組件。Called before the assembly is loaded to allow the implementing class to modify the assembly.