IAssemblyPostProcessor 介面

定義

定義類別實作的方法,以在建置元件之後處理元件。

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

範例

以下程式碼範例示範如何建立介面實作 IAssemblyPostProcessor ,並將其註冊在網頁應用程式的 Web.config 檔案中。

程式碼範例的第一部分建立一個名為 Samples.Process.postProcessTest class 的類別,實作介面。IAssemblyPostProcessor 這個類別執行在呼叫該方法時 PostProcessAssembly 寫入檔案的簡單動作。

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:");
            sw.WriteLine(path);
            sw.Close();
        }

        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:")
            sw.WriteLine(path)
            sw.Close()
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose

        End Sub
    End Class
End Namespace

將類別編譯成一個 .dll 檔案,指令為 csc /target:library postProcessTest.cs。 將產生的 .dll 檔案加入 ASP.NET 應用程式的 Bin 資料夾,並在 Web.config 檔案中註冊該 .dll,如下程式碼所示。

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

當使用者造訪網站時,網頁應用程式會被動態編譯,檔案 MyTest.txt 會寫入 C:\compile。

備註

實作此介面的類別可在編譯後存取該組合語言。 該 AssemblyBuilder 類別編譯組件,然後檢查介面是否 IAssemblyPostProcessor 已在網頁設定檔中註冊。 若是如此, AssemblyBuilder 實例會呼叫 PostProcessAssembly 介面方法 IAssemblyPostProcessor ,在編譯後且載入組裝前執行任何動作。 例如,分析工具可以實作此介面,在組件中建立探針。

當註冊 IAssemblyPostProcessor 介面時,ASP.NET 應用程式及其組件總是以除錯模式編譯。

方法

名稱 Description
Dispose()

執行與釋放、釋放或重設非受控資源相關聯的應用程式定義工作。

(繼承來源 IDisposable)
PostProcessAssembly(String)

在載入組合語言前呼叫,讓實作類別修改該組合語言。

適用於