的FxCop:用于检查装配信息值自定义规则 [英] FxCop: custom rule for checking assembly info values

查看:136
本文介绍了的FxCop:用于检查装配信息值自定义规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个相当简单的方式来获得的FxCop来检查我所有的组件声明特定属性值?我想确保每个人都已经改变了你对创建项目的默认值:

  [汇编:AssemblyCompany(微软) ] //失败

[总成:AssemblyCompany(FooBar的公司)] //通


< DIV CLASS =h2_lin>解决方案

这其实是一个非常简单的规则,一旦你知道的FxCop的最大的分析对象是一个模块,而不是组装。在大多数情况下,有每个装配一个模块,所以这不会引起问题。但是,如果你得到每个装配重复的问题,通知,因为你有每个装配多个模块,您可以添加一个检查,以防止产生每个装配多个问题。



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

 私人TypeNode AssemblyCompanyAttributeType {搞定;组; } 

公共覆盖无效BeforeAnalysis()
{
base.BeforeAnalysis();

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

公众覆盖ProblemCollection检查(ModuleNode模块)
{
AttributeNode assemblyCompanyAttribute = module.ContainingAssembly.GetAttribute(this.AssemblyCompanyAttributeType);
如果(assemblyCompanyAttribute == NULL)
{
this.Problems.Add(新问题(this.GetNamedResolution(NoCompanyAttribute),模块));
}
,否则
{
字符串的companyName =(字符串)((文字)assemblyCompanyAttribute.GetPositionalArgument(0))值。
如果(!string.Equals(公司名称,FooBar的公司,StringComparison.Ordinal))
{
this.Problems.Add(新问题(this.GetNamedResolution(WrongCompanyName的companyName),模块));
}
}

返回this.Problems;
}


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

解决方案

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

这篇关于的FxCop:用于检查装配信息值自定义规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆