IAssemblyPostProcessor 接口

定义

定义类在生成程序集后要处理程序集的方法。

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

示例

下面的代码示例演示如何创建接口的 IAssemblyPostProcessor 实现,并将其注册到 Web 应用程序的 Web.config 文件中。

代码示例的第一部分创建一个名为 Samples.Process.postProcessTest 实现接口的 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

使用命令 csc /target:library postProcessTest.cs将类编译为 .dll 文件。 将生成的 .dll 文件添加到 ASP.NET 应用程序的 Bin 文件夹中,并在 Web.config 文件中注册 .dll,如以下代码所示。

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

当用户访问网站时,将动态编译 Web 应用程序,并将文件 MyTest.txt 写入 C:\compile。

注解

实现此接口的类可以在编译程序集后访问程序集。 该 AssemblyBuilder 类编译程序集,然后检查接口是否已 IAssemblyPostProcessor 在 Web 配置文件中注册。 如果是这样,实例 AssemblyBuilder 将调用 PostProcessAssembly 接口的方法 IAssemblyPostProcessor ,以便在编译之后以及加载程序集之前执行任何操作。 例如,探查器工具可以实现此接口以在程序集中建立探测。

注册 IAssemblyPostProcessor 接口时,ASP.NET 应用程序及其程序集将始终在调试模式下编译。

方法

名称 说明
Dispose()

执行与释放、释放或重置非托管资源关联的应用程序定义任务。

(继承自 IDisposable)
PostProcessAssembly(String)

在加载程序集之前调用,以允许实现类修改程序集。

适用于