views:

33

answers:

2

Is there a simple way to identify Reflection.Emit-generated assemblies? When processing all assemblies loaded into an application domain, Assembly instances of dynamically generated assemblies don't behave the same as for standard assemblies. For example, accessing the CodeBase property leads to an exception:

string codeBase;
try
{
    codeBase = assembly.CodeBase;
}
catch(NotSupportedException)
{
    // assemblies generated via Reflection.Emit throw an exception when CodeBase is accessed
    codeBase = null;
}

Is there a better way to recognize this situation and avoid the try … catch block?

A: 

Doesn't Assembly.IsDynamic answer your question? It may be that it is new in .Net 4.0.

Virtlink
That's it. My code is running in a .NET 3.5 environment.
Ondrej Tucny
A: 

This should work:

if (assembly is System.Reflection.Emit.AssemblyBuilder) {
    // It's dynamic
    //...
}
Hans Passant