2011-11-11 33 views

回答

4

這其實是一個非常簡單的規則,一旦你知道的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; 
} 
+0

精湛,謝謝! –

相關問題