简体   繁体   English

FxCop:用于检查程序集信息值的自定义规则

[英]FxCop: custom rule for checking assembly info values

Is there a reasonably simple way to get FxCop to check that all my assemblies declare a particular attribute value? 是否有一种相当简单的方法让FxCop检查所有程序集是否声明了特定的属性值? I want to make sure everyone has changed the default you get on creating a project: 我想确保每个人都更改了创建项目时的默认值:

[assembly: AssemblyCompany("Microsoft")] // fail

[assembly: AssemblyCompany("FooBar Inc.")] // pass

This is actually a pretty easy rule once you know that FxCop's "largest" analysis target is a module, not an assembly. 一旦您知道FxCop的“最大”分析目标是模块而不是程序集,这实际上是一个非常简单的规则。 In most cases, there is one module per assembly, so this won't pose a problem. 在大多数情况下,每个组件有一个模块,因此这不会造成问题。 However, if you are getting duplicate problem notifications per assembly because you do have multiple modules per assembly, you can add a check to prevent generating more than one problem per assembly. 但是,如果每个程序集都有重复的问题通知,因为每个程序集都有多个模块,则可以添加一个检查以防止每个程序集生成多个问题。

At any rate, here's the basic implementation of the rule: 无论如何,这是规则的基本实现:

private TypeNode AssemblyCompanyAttributeType { get; set; }

public override void BeforeAnalysis()
{
    base.BeforeAnalysis();

    this.AssemblyCompanyAttributeType = FrameworkAssemblies.Mscorlib.GetType(
                                            Identifier.For("System.Reflection"),
                                            Identifier.For("AssemblyCompanyAttribute"));
}

public override ProblemCollection Check(ModuleNode module)
{
    AttributeNode assemblyCompanyAttribute = module.ContainingAssembly.GetAttribute(this.AssemblyCompanyAttributeType);
    if (assemblyCompanyAttribute == null)
    {
        this.Problems.Add(new Problem(this.GetNamedResolution("NoCompanyAttribute"), module));
    }
    else
    {
        string companyName = (string)((Literal)assemblyCompanyAttribute.GetPositionalArgument(0)).Value;
        if (!string.Equals(companyName, "FooBar Inc.", StringComparison.Ordinal))
        {
            this.Problems.Add(new Problem(this.GetNamedResolution("WrongCompanyName", companyName), module));
        }
    }

    return this.Problems;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM