Domanda

I am trying the create a custom fxcop rule which checks for all the methods in target assembly having their names NOT starting with the CAPITAL letter. I am pretty successful in doing this but there is one problem. The rule throws error for "delegate methods" as well, for ex. btnOk_Click which I don't want, is there any way to identify/filter delegate methods in fxcop using any predefined property/method ?

È stato utile?

Soluzione

An idea would be to write custom code rules through the tool NDepend instead (Disclaimer: I am one of the developer of the tool).

NDepend is especially conceived to make easy custom code rules edition through LINQ query. The following Code Query LINQ (CQLinq) query covers your need:

// <Name>Method name MUST start with CAPITAL</Name>
warnif count > 0 
from m in Application.Assemblies.WithName("TargetAssemblyName").ChildMethods()
where 
  !m.IsSpecialName &&         // Remove getter and setter
  !m.IsGeneratedByCompiler && // Discard methods generated by compiler
  !m.ParentType.IsDelegate &&
  !m.NameLike("^btn") &&      // Use regex here to discard btnOk_Click like method
  !char.IsUpper(m.SimpleName[0])
select m

Just write this code rule in NDepend query editor in VS, and get an immediate feedback:

NDepend Custom Code Rule

NDepend code rule can be executed/validated live in VS, or can be executed at Build Process time and validated in a report.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top