Is it possible to get all the referenced assemblies (within a Unit Test project) that have a custom attribute applied. I use the following code withing my application which works successfully:
var assemblies = System.Web.Compilation.BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(a => a.GetCustomAttributes(false).OfType<AssemblyCategoryAttribute>().Any()).ToList();
However System.Web.Compilation.BuildManager doesn’t work in my test project so I tried:
Assembly.GetExecutingAssembly().GetReferencedAssemblies().Select(a => Assembly.ReflectionOnlyLoad(a.FullName).Where(a => a.GetCustomAttributes(false).OfType<AssemblyCategoryAttribute>().Any()).ToList();
But this threw the error:
It is illegal to reflect on the custom attributes of a Type loaded via
ReflectionOnlyGetType (see Assembly.ReflectionOnly) — use
CustomAttributeData instead.
I’d appreciate it if someone could show me how to do this. Thanks
Since you’re getting the referenced assemblies for the currently executing assembly, there is no reason to do a reflection-only load.
ReflectionOnlyLoadis for when you want to look at the assemblies but not actually execute them. Since these assemblies are being referenced by the currently executing assembly, there most likely either are or will be loaded into the execution context anyway.Try just doing:
Or better yet: