Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a .NET assembly is dynamic?

When iterating through a set of assemblies, e.g. AppDomain.CurrentDomain.GetAssemblies(), dynamic assemblies will throw a NotSuportedException if you try to access properties like CodeBase. How can you tell that an assembly is dynamic without triggering and catching the NotSupportedException?

like image 507
Mike Stockdale Avatar asked Sep 14 '09 20:09

Mike Stockdale


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.

Which namespace of the .NET framework do you use to create metadata dynamically at runtime?

Emit namespace that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) at run time and optionally generate a portable executable (PE) file on disk. Script engines and compilers are the primary users of this namespace.

Which of the following namespace is required to create a dynamic assembly and optionally generate PE file on a disk?

Emit Namespace. Contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk.


1 Answers

To check if the assembly is dynamic:

if (assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder) 

This took me a while to figure out, so here it is asked and answered.

Update:

In .NET 4.0, there is now a property:

if (assembly.IsDynamic) 
like image 70
Mike Stockdale Avatar answered Sep 27 '22 21:09

Mike Stockdale