FxCop: custom rule for checking assembly info values

筅森魡賤 提交于 2019-12-19 18:29:57

问题


Is there a reasonably simple way to get FxCop to check that all my assemblies declare a particular attribute value? 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

回答1:


This is actually a pretty easy rule once you know that FxCop's "largest" analysis target is a module, not an assembly. 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;
}


来源:https://stackoverflow.com/questions/8091821/fxcop-custom-rule-for-checking-assembly-info-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!