无法卸载.NET Core 3中的DLL

无法卸载.NET Core 3中的DLL,dll,.net-core,.net-assembly,Dll,.net Core,.net Assembly,我试图卸载一个外部程序集,但它仍然在内存中,我无法删除dll文件。这是我的代码-我做错了什么 上传的dll非常简单-只有一个类和一个方法。没有依赖关系 我查看了许多示例,发现代码中没有任何问题,但仍然不起作用 谢谢大家! class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext { public SimpleUnloadableAssemblyLoadContext( ) : base(isCollectible: t

我试图卸载一个外部程序集,但它仍然在内存中,我无法删除dll文件。这是我的代码-我做错了什么

上传的dll非常简单-只有一个类和一个方法。没有依赖关系

我查看了许多示例,发现代码中没有任何问题,但仍然不起作用

谢谢大家!

class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext
{
    public SimpleUnloadableAssemblyLoadContext( ) : base(isCollectible: true)
    {
    }
    protected override Assembly Load(AssemblyName name)
    {
        return null;
    }
}

public class PluginLoader2
{

    [MethodImpl(MethodImplOptions.NoInlining)]
    public string getExternalText()
    {
        string res = "";
        var sourcesPath = Path.Combine(Environment.CurrentDirectory, "Plugins");

        string[] fileEntries = Directory.GetFiles(sourcesPath);
        foreach (string fileName in fileEntries)
        {
            SimpleUnloadableAssemblyLoadContext context = new SimpleUnloadableAssemblyLoadContext();
            WeakReference w_r = new WeakReference(context, trackResurrection: true);

            var myAssembly = context.LoadFromAssemblyPath(fileName);

            context.Unload();
            for (var i = 0; i < 10 && w_r.IsAlive; i++)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            res += "<br /><br />" + fileName + " live status is " + w_r.IsAlive.ToString();
        }

        return res;
    }
}
类SimpleUnloadableAssemblyLoadContext:AssemblyLoadContext
{
公共SimpleUnloadableAssemblyLoadContext():base(isCollectible:true)
{
}
受保护的替代程序集加载(程序集名称)
{
返回null;
}
}
公共类插件装入器2
{
[MethodImpl(MethodImplOptions.noInLine)]
公共字符串getExternalText()
{
字符串res=“”;
var sourcesPath=Path.Combine(Environment.CurrentDirectory,“Plugins”);
string[]fileEntries=Directory.GetFiles(sourcesPath);
foreach(文件项中的字符串文件名)
{
SimpleUnloadableAssemblyLoadContext=新SimpleUnloadableAssemblyLoadContext();
WeakReference w_r=新的WeakReference(上下文、轨迹复活:true);
var myAssembly=context.LoadFromAssemblyPath(文件名);
Unload();
对于(变量i=0;i<10&&w\r.IsAlive;i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
res+=“

”+fileName+“实时状态为”+w\r.IsAlive.ToString(); } 返回res; } }
请不要在提问时使用标签。而且也不是关于.NETCore的。阅读标记弹出窗口,不要只是在标记字段中键入随机文本。因此,要澄清的是,您的问题是希望在加载程序集后删除它?是-卸载它。由于某些原因,此代码不会卸载它。