【发布时间】:2020-09-06 18:13:01
【问题描述】:
我不明白为什么在这种情况下上下文没有卸载
class Program
{
static void Main(string[] args)
{
CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
Assembly assembly = context.LoadFromAssemblyPath(@"C:\Users\Greedy\source\repos\ConsoleApp1\MyApp\bin\Debug\netcoreapp3.1\MyApp.dll");
context.Unload();
GC.Collect();
GC.WaitForPendingFinalizers();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
Console.WriteLine(asm.GetName().Name);
Console.Read();
}
但现在上下文上下文已成功卸载。
class Program
{
static void Main(string[] args)
{
CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
Load(context);
context.Unload();
GC.Collect();
GC.WaitForPendingFinalizers();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
Console.WriteLine(asm.GetName().Name);
Console.Read();
}
static void Load(CustomAssemblyLoadContext context)
{
Assembly assembly = context.LoadFromAssemblyPath(@"C:\Users\Greedy\source\repos\ConsoleApp1\MyApp\bin\Debug\netcoreapp3.1\MyApp.dll");
}
为什么?以及如何在第一个示例中修复卸载?
【问题讨论】:
-
什么是
CustomAssemblyLoadContext?
标签: c# .net .net-assembly