views:

200

answers:

1

I'm getting the Product attribute from all the loaded assemblies in my application using:

AssemblyProductAttribute product
    = (AssemblyProductAttribute)Attribute.GetCustomAttribute(
        assembly, typeof(AssemblyProductAttribute));

I would like to get this attribute for all the assemblies that the currently loaded assemblies reference. However GetReferencedAssemblies() returns an array of AssemblyNames, so I can't use the above code to get the Product attribute.

Is there a way to get an Assembly object from an AssemblyName object, or a way to get the Product attribute from an AssemblyName?

+1  A: 

Well you can use Assembly.Load(AssemblyName) to load the assembly - is that good enough for you? Note that once you've loaded the assembly, you won't be able to unload it other than by unloading the AppDomain. Of course, if those assemblies were going to be loaded anyway, there's no harm done. (Once you've loaded the assembly once into an AppDomain, using the same AssemblyName again will just return the already-loaded assembly.)

Jon Skeet
I believe this should work--I was not sure what the effects of loading an already-loaded assembly would be.
emddudley
There shouldn't be an problems, as far as I'm aware - it should return the already-loaded assembly. Will check when I get the chance.
Jon Skeet
This solution is not perfect because it turns out that some of my assemblies are referencing other assemblies that are not being included with my software, triggering a FileNotFoundException.
emddudley
Surely that's just showing that you have a problem with your assemblies: either you should ship those assemblies, or not depend on them. If you absolutely *have* to have them, I would suggest having a hard-coded list of "ignored assemblies".
Jon Skeet