How to tell if an Assembly is dynamic?

  • Thread starter Thread starter Samuel R. Neff
  • Start date Start date
S

Samuel R. Neff

I'm looping through assemblies and checking CodeBase prior to
processing a given assembly. However, I occasionally get this error:

The invoked member is not supported in a dynamic module.

Which makes sense, but how can I check for this condition before
calling CodeBase (i.e., check for the error condition before
triggering the error). I didn't see any related properties.
Assembly.Location gave the same error.

Thanks,

Sam


foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
if (Path.GetDirectoryName(asm.CodeBase) != localDirectoryName)
{
continue;
}

foreach (Type t in asm.GetExportedTypes())
{
...
}
}
 
Samuel,

You might be able to enumerate through the Evidence for the assembly and
look for the Url class in the System.Security.Policy namespace. From that
instance that is returned, (or not) you can check the location (or lack of
one, if no instance is returned).

There is no way to tell it to give you the Url instance, so you will
have to enumerate through each instance returned by the IEnumerable
implementation and check the type to see if it is a Url instance, and then
check the properties if it is.

I'm not really sure if this is a foolproof way of checking to see if an
assembly is dynamically created though, as the location or site could change
depending on the host. For example, in Sql Server, there is no file-based
path, or url for the assemblies loaded, they are all retrieved from the
database itself.
 
Back
Top