簡體   English   中英

FxCop:用於檢查程序集信息值的自定義規則

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

是否有一種相當簡單的方法讓FxCop檢查所有程序集是否聲明了特定的屬性值? 我想確保每個人都更改了創建項目時的默認值:

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

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

一旦您知道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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM