Customizing the Microsoft .Net Framework Common Language Runtime by Steven Pratschner
I’ve been wanting to read this book for some time, and finally bought it for six pounds over Christmas. The CLR is designed to be used as a library, sharing the address space with other code (the host). This isn’t particularly novel – the Lisp system I worked on in the 1990s could be saved as a dll which could be loaded into another process. What is novel about the CLR is that it offers a set of interfaces that allow the host process to control its actions. Everything from thread scheduling (and hence access to synchronisation primitives), threadpool implementation and memory allocation can be controlled by the host by implementing a number of interfaces that the CLR calls out to when it needs to do certain actions. The host can interact with the CLR, asking it to garbage collect and warning it of low memory conditions. Moreover the host can customise the assembly loading process and the security sandbox in which these assemblies run, and also use HostProtection, surfaced into the libraries via the HostProtectionAttribute, to constrain what the code running in the CLR is allowed to do.
The book covers add-ins really well, and as part of this has a couple of really good chapters on Application Domains. Why they exist, how they are configured, loaded and unloaded and how they can be used effectively are all covered in good detail. The book looks into how the host can protect itself against badly behaved add-ins, preventing resource leakage using critical regions and critical finalisers. These are not really covered very well in other books I have read.
The book is a little dry, but does contain some interesting examples, and gives a good glimpse into what the CLR is doing behind the scenes.