This is a mirror of official site: http://jasper-net.blogspot.com/

Load a .NET Assembly into a Separate AppDomain So You Can Unload It

| Sunday, October 24, 2010
There may be times when you wish to temporarily load a .NET assembly to inspect it, but you don’t want the assembly to remain in your program’s memory taking up resources.  Unfortunately, once your program loads an assembly, there is no way to unload it.  The best way is to create a separate AppDomain, load the assembly into that AppDomain, then unload the AppDomain when you are finished.
The following sample code loads a .NET assembly from disk, displays the name of every type defined in the assembly, then unloads the assembly:

AppDomain appDomain = null;
try
{
   string path = @"C:\myAssembly.dll";
   byte[] buffer = File.ReadAllBytes( path );

   appDomain = AppDomain.CreateDomain( "Test" );
   Assembly assm = appDomain.Load( buffer );

   Type[] types = assm.GetTypes();
   foreach (Type type in types)
   {
       Console.WriteLine( type.FullName );
   }
}
catch (Exception ex)
{
   Console.WriteLine( ex.Message );
}
finally
{
   if (appDomain != null)
       AppDomain.Unload( appDomain );
}

Read more: C# 411

Posted via email from .NET Info

0 comments: