Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net dynamic assemblies

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.

like image 476
Peanut Avatar asked Mar 07 '10 00:03

Peanut


People also ask

What is a dynamic assembly?

Dynamic assemblies are those assemblies which are not stored on the disk before execution in fact after execution they get stored on the disk. When .NET runtime calls them they are directly loaded from the memory not from the disk.

Is assemblies can only be dynamic?

Assemblies can be static or dynamic. Static assemblies are stored on disk in portable executable (PE) files. Static assemblies can include interfaces, classes, and resources like bitmaps, JPEG files, and other resource files.

How does .NET resolve assembly references?

If the runtime determines that an assembly matches the calling assembly's criteria, it uses that assembly. When the file specified by the given <codeBase> element is loaded, the runtime checks to make sure that the name, version, culture, and public key match the calling assembly's reference.

What is assembly in .NET and its types?

In the Microsoft . NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security. There are two types: process assemblies (EXE) and library assemblies (DLL). A process assembly represents a process which will use classes defined in library assemblies. .


1 Answers

I'll give you some examples:

  1. 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.

  2. 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.

  3. 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].

  4. 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?

like image 110
Cheeso Avatar answered Oct 20 '22 11:10

Cheeso