[英]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.