We all know that the assembly can be queried for attributes using the GetCustomAttributes method. I want to use this to identify an extension module for my application. However, to avoid loading every assembly I prefer a defensive approach:
using Assembly.ReflectionOnlyLoadFrom to get more details about an assembly (has it my ModuleAttribute?)
if the ModuleAttribute is found, I will finally load it using Assembly.LoadFrom
Unfortunately it seems that there is no way to get the attributes from an assembly, that is loaded into the reflection-only context:
myAssembly.GetCustomAttributes(typeof(ModuleAttribute), false)
fails with an InvalidOperationException ("It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType") and
CustomAttributeData.GetCustomAttributes(myAssembly)
fails with ReflectionTypeLoadException because of dependant assemblies not being loaded.
So how to get the attributes without
- polluting my application domain with useless (maybe harmful) types by calling Assembly.LoadFrom
- the need to load all referenced assemblies
- the need for separate application domains (gave it a short try, smelled like even more PITA)
?