Check if the assembly is unloaded in this test:
private void button1_Click( object sender, EventArgs e )
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies( );
WeakReference awr;
ExecuteAndUnload( out awr );
for( int i = 0; awr.IsAlive && ( i < 10 ); i++ )
{
GC.Collect( );
GC.WaitForPendingFinalizers( );
}
}
[MethodImpl( MethodImplOptions.NoInlining )]
static void ExecuteAndUnload( out WeakReference alcWeakRef )
{
string code = @"public class Person
{
public static string SayName()=>""fan"";
}";
AssemblyLoadContext assemblyLoadContext = new AssemblyLoadContext( "alc", true );
alcWeakRef = new WeakReference( assemblyLoadContext, trackResurrection: true );
var compiler = new Compiler( );
var assembly = compiler.Compile( code, assemblyLoadContext, Assembly.Load( new AssemblyName( "System.Runtime" ) ), typeof( object ).Assembly );
assemblyLoadContext.Unload( );
See: