I was recently asked if I knew anything about Dynamic Assemblies in .Net. The short answer was - I don't.
I have found plenty of articles that describe how to create a dynamic assembly but none that truely explain the following:
- What they are (other than they are run directly from memory)
- What advantages they provide over static assemblies
- Real world examples of their usage
Any explanations to the above would be very much appreciated.
Many thanks.
I'll give you some examples:
ASPNET generates assemblies and loads them dynamically, for each ASPX, ASMX, or ASHX. The real benefit here is that the application code can be deployed in a template language, and can get dynamically compiled and run on demand. The dynamic part makes for a very simple and convenient deployment model, and also means efficiency: Only the pages that get called are actually loaded.
DotNetZip creates a dynamic assembly when saving a Self-Extracting archive. Actually it isn't "run from memory", it is written to a file, eventually, so this may or may not fit your definition for a dynamic assembly. The assembly is created dynamically, at runtime. But it is not invoked at that moment. Why create it dynamically? Because the exe needs to be able to use a particular Win32 Icon, it might need a version number and other properties. Those things can be set at compile time. Also, the source code for the assembly is derived from a template, using various data provided by the caller to fill in slots in the template. So a dynamically generated assembly is really the right way to go here.
In .NET's ASMX web services framework, the wsdl.exe compiler (or the xsd.exe tool) would produce types to serialize/deserialize XML messages. It would typically emit types that had the XML elements modelled as public fields. But while the DataGrid and other data-bound controls could use arrays of objects as data sources, they displayed only public properties. Therefore an app could not perform a webservices call, get back an array of objects, then assign that as the source for a Datagrid. I used a dynamically generated assembly as an Adapter to allow data-driven controls to consume the output of webservices calls. [This problem has since gone away, I think, with the ObjectDataSource and other changes in .NET].
Internally in .NET, instantiating the System.Xml.Serialization.XmlSerializer class for a particular type generates an assembly dynamically. I suppose the win here is the same as for any interpeted-vs-compiled code comparison. in xml serialization, the basic idea is to enumerate through a type's public fields and properties, and then emit an XML document that contains the values from those fields and props. Wouldn't it be nice if the app didn't have to use System.Reflection to enumerate a type's members (very slooooow), each time XmlSerializer.Serialize() is called?
Here's a recent SO question describing a scenario where someone wants to create a dynamic assembly:
How to use code generation to dynamically create C# methods?
This article is a bit dated and the code is a bit 'rustic' but I would say it is one of the most accessible articles regarding dynamic compilation and some of the salient issues.
It is where I learned to compile assemblies on the fly, both in memory when I did not need to unload or control security, and to a temp file to be loaded with remoting to allow unloading.
A real world example: An online .net regex tool that accepts c# code for a replacer method that is dynamically compiled into a sandboxed assembly, used to perform the replacement and discarded. This strategy worked just fine, but the possibility of malicious code being injected regardless of sandboxing was just too great so the idea was eventually scrapped.
Good luck.