How can I check for an InternalsVisibleTo attribute on an assembly?

I've used ILMerge to merge a secondary assembly that is itself merged with Castle.DynamicProxy, having set most if not all of the normally-public Castle types to internal. When I use the code from the secondary assembly that is dependent on the Castle types, I get a TypeLoadException saying that access is denied.

The first step to check is that my merged assembly has the InternalsVisibleTo attribute still set for the DynamicProxy2 assembly. Any way to check this?

Jon Skeet
people
quotationmark

Assuming you can load the assembly itself, you can use Assembly.GetCustomAttributes:

var asm = ...;
var internals = asm.GetCustomAttributes(typeof(InternalsVisibleToAttribute),
                                        false);
var foundDynamicProxy2 = internals.Cast<InternalsVisibleToAttribute>()
                                  .Any(x => x.AssemblyName == "DynamicProxy2");

people

See more on this question at Stackoverflow