5
是否有合理簡單的方法讓FxCop檢查我的所有程序集是否聲明特定的屬性值?我想確保每個人都已經改變你的默認創建一個項目:FxCop:用於檢查程序集信息值的自定義規則
[assembly: AssemblyCompany("Microsoft")] // fail
[assembly: AssemblyCompany("FooBar Inc.")] // pass
是否有合理簡單的方法讓FxCop檢查我的所有程序集是否聲明特定的屬性值?我想確保每個人都已經改變你的默認創建一個項目: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;
}
精湛,謝謝! –