IAssemblyPostProcessor 接口

定义

定义方法,类可实现这些方法在生成程序集之后对程序集进行处理。

C#
public interface IAssemblyPostProcessor : IDisposable
实现

示例

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

代码示例的第一部分创建一个名为 Samples.Process.postProcessTest 的类,该 IAssemblyPostProcessor 类实现 接口。 调用 方法时 PostProcessAssembly ,此类执行编写文件的简单操作。

C#
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()
        {
        }
    }
}

使用 命令 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 调用 PostProcessAssemblyIAssemblyPostProcessor 接口的 方法,以便在编译后和加载程序集之前执行任何操作。 例如,探查器工具可以实现此接口以在程序集中建立探测。

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

方法

Dispose()

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

(继承自 IDisposable)
PostProcessAssembly(String)

在加载程序集之前调用,可允许实现类对程序集进行修改。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1