1

我们正在对大量(数千个)C# 项目进行广泛的代码分析。但是尽管我们没有调用 Solution.Emit() 方法,程序集仍然被加载到内存中。还有其他方法也会导致这种行为吗?甚至可以在不将程序集加载到内存中的情况下创建编译并检索其符号吗?希望我们能以某种方式避免创建单独的应用程序域并卸载它们。

4

1 回答 1

1

看起来您的方法是错误的,您可能会在主 AppDomain 中加载程序集。

但是文档如何:加载和卸载程序集说要卸载程序集,您需要卸载加载这些程序集的完整应用程序域。它还链接到如何:卸载应用程序域

所以你应该:

  1. 为程序集创建一个新的应用程序域
  2. 将程序集加载到新的 AppDomain
  3. 进行代码分析
  4. 卸载该程序集特定的应用程序域

这是文档中的代码:

Console.WriteLine("Creating new AppDomain.");
AppDomain domain = AppDomain.CreateDomain("MyDomain", null);

Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("child domain: " + domain.FriendlyName);
try
{
    AppDomain.Unload(domain);
    Console.WriteLine();
    Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
    // The following statement creates an exception because the domain no longer exists.
    Console.WriteLine("child domain: " + domain.FriendlyName);
}
catch (AppDomainUnloadedException e)
{
    Console.WriteLine(e.GetType().FullName);
    Console.WriteLine("The appdomain MyDomain does not exist.");
}
于 2021-10-28T08:45:39.093 回答