views:

257

answers:

1

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?

+5  A: 

If (assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder), the assembly is dynamic.

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

Update:

In .NET 4.0, it looks like the class name has changed, so this seems to be more reliable:

if (assembly.ManifestModule.GetType().Namespace == "System.Reflection.Emit")

Mike Stockdale
Revised for .NET 4.0
Mike Stockdale