【问题标题】:FxCop: custom rule for checking assembly info valuesFxCop:检查程序集信息值的自定义规则
【发布时间】:2011-11-11 09:16:00
【问题描述】:

是否有一种相当简单的方法可以让 FxCop 检查我的所有程序集是否声明了特定的属性值?我想确保每个人都更改了您在创建项目时获得的默认设置:

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

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

【问题讨论】:

    标签: c# fxcop fxcop-customrules


    【解决方案1】:

    一旦您知道 FxCop 的“最大”分析目标是一个模块,而不是一个程序集,这实际上是一个非常简单的规则。在大多数情况下,每个组件都有一个模块,因此这不会造成问题。但是,如果您因为每个程序集有多个模块而收到每个程序集的重复问题通知,则可以添加一项检查以防止每个程序集生成多个问题。

    无论如何,这是该规则的基本实现:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多