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

坚强是说给别人听的谎言 提交于 2020-01-05 08:07:10

问题


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?


回答1:


DotPeek shows this by double-clicking the assembly. [assembly: InternalsVisibleTo(...)] should be visible zero or more times.

Also, Ildasm should be able to tell you this by double-clicking the MANIFEST of the assembly. A yellow popup shows lines that start with .custom instance void [mscorlib]System.Runtime.CompilerServices.InternalsVisibleToAttribute.




回答2:


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");


来源:https://stackoverflow.com/questions/28700047/how-can-i-check-for-an-internalsvisibleto-attribute-on-an-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!