代码分析 AssemblyNode.GetType始终返回null

发布于 2024-12-26 07:04:13 字数 751 浏览 2 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2025-01-02 07:04:13

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 using assembly.GetAttribute instead. For an example, see FxCop: custom rule for checking assembly info values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文