There was discussion at work about assembly unloading. In order to get the CLR to unload an assembly, you need to use a fresh AppDomain and only load a given type into that AppDomain. When the AppDomain is unloaded, the Assembly’s reference count is decremented, and the Assembly can be unloaded.
It is fairly easy to use windbg to track the module load and unload events to check this behaviour. We can build an Assembly that we intend to load into a new AppDomain.
We can then write a Main method which creates a new AppDomain and then loads this Assembly in to it.
If we run this executable from Windbg, as long as we have set the event filtering to output module unloads (which isn’t the default behaviour), we see the module is loaded into the AppDomain and then unloaded after the AppDomain is killed.
All as expected, but it is good to check one’s expectations from time to time.