mardi 30 mai 2023

In .NET 6.0/7.0, how to load an assembly and then delete the assembly file?

In .NET 6.0, I loaded an assembly and invoked its methods and properties using reflection, then tried to delete the assembly file:

    var dllFilePath = @"C:\Users\frank\source\repos\.NET core\MyDll\bin\Debug\net6.0\MyDll.dll";
    var ctx = new AssemblyLoadContext(null, isCollectible: true);
    var assembly = ctx.LoadFromAssemblyPath(dllFilePath);
    var myType = assembly.GetType("MyDll.MyClass");
    var instance = Activator.CreateInstance(myType, new object[] { }, null);
    var myMethod = myType.GetMethod("GetGuidText");
    var res = myMethod.Invoke(instance, new object[] { Guid.NewGuid() });

    ctx.Unload();
    myType = null;
    instance = null;
    myMethod = null;
    ctx = null;
    assembly = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    Thread.Sleep(2000);
    GC.Collect();
    GC.WaitForPendingFinalizers();

    File.Delete(dllFilePath);

The attempt to delete threw exception:

System.UnauthorizedAccessException: 'Access to the path 'C:\...\MyDll\bin\Debug\net6.0\MyDll.dll' is denied.'

How do I delete this file without having to restart the app?





Aucun commentaire:

Enregistrer un commentaire