5

すべてのアセンブリが特定の属性値を宣言していることを FxCop に確認させる簡単な方法はありますか? プロジェクトの作成時に取得するデフォルトを全員が変更したことを確認したいと思います。

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

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

1 に答える 1

4

これは、FxCop の "最大の" 分析対象がアセンブリではなくモジュールであることを理解していれば、実際には非常に簡単なルールです。ほとんどの場合、アセンブリごとに 1 つのモジュールがあるため、これが問題になることはありません。ただし、アセンブリごとに複数のモジュールがあるためにアセンブリごとに重複した問題通知を受け取る場合は、チェックを追加して、アセンブリごとに複数の問題が発生しないようにすることができます。

とにかく、ルールの基本的な実装は次のとおりです。

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;
}
于 2011-11-11T12:54:51.393 に答える