Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...