代码分析 AssemblyNode.GetType始终返回null
我正在为 Visual Studio 2010(基本上是 FxCop,但最新版本)编写自定义代码分析规则。我试图使用如下代码来获取应用于正在检查的程序集的属性(或所有属性的集合):
public override ProblemCollection Check( ModuleNode module )
{
AssemblyNode assembly = module as AssemblyNode;
if ( assembly != null )
{
Identifier ns = Identifier.For( "System.Reflection" );
Identifier attr = Identifier.For( "AssemblyCopyrightAttribute" );
TypeNode type = assembly.GetType( ns, attr );
...
...但是“类型”始终为空,即使我知道事实上这样的属性是为程序集定义的。
此外...当我调试这个时,我看到 assembly.ModuleAttributes 集合是空的,ExportedTypes 也是空的,Modules 也是空的...看起来该程序集根本不包含任何内容!然而,“基本”ModuleNode 已完全填充,例如在其属性集合中包含 14 个属性。
就好像“module as AssemblyNode”是错误的,但如果是这样,它将返回 null!谁能解释我做错了什么?
I am writing custom Code Analysis rules for Visual Studio 2010 (basically FxCop but the newest version). I am trying to get an attribute (or, a collection of all the attributes) applied to the assembly being checked, using code like the following:
public override ProblemCollection Check( ModuleNode module )
{
AssemblyNode assembly = module as AssemblyNode;
if ( assembly != null )
{
Identifier ns = Identifier.For( "System.Reflection" );
Identifier attr = Identifier.For( "AssemblyCopyrightAttribute" );
TypeNode type = assembly.GetType( ns, attr );
...
...but 'type' is always null, even when I know for fact that such an attribute is defined for the assembly.
Furthermore... when I debug this, I see that the assembly.ModuleAttributes collection is empty, as is ExportedTypes, as is Modules... it looks as though the assembly contains nothing at all! However, the 'base' ModuleNode is fully populated, and for example does contain 14 Attributes in its attribute collection.
It's as though "module as AssemblyNode" is wrong, but if so it would return null! Can anyone explain what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ModuleNodel.GetType
查找类型的定义,而不是使用类型。 AssemblyCopyrightAttribute 在 mscorlib 程序集中定义,这可能不是您的规则的目标。要查找 AssemblyCopyrightAttribute 的用途,请尝试改用assemble.GetAttribute
。有关示例,请参阅 FxCop:用于检查程序集信息值的自定义规则。ModuleNodel.GetType
looks for definition of the type, not use of the type. AssemblyCopyrightAttribute is defined in the mscorlib assembly, which is probably not what your rule is targeting. To find uses of AssemblyCopyrightAttribute, try usingassembly.GetAttribute
instead. For an example, see FxCop: custom rule for checking assembly info values.