1

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 ?

Sagar
  • 645
  • 2
  • 9
  • 31
  • You mean normal methods that you create delegates for – SLaks Dec 04 '12 at 15:48
  • @SLaks : In simple terms , the methods that we register for any event, like for button click event we have a btnOk_Click methods, these are also treated as method in Fxcop when we convert a Member to method inside check method – Sagar Dec 04 '12 at 16:16
  • FxCop is for static analysis (detecting unreachable code and the like) whereas what you want to do is enforce a coding standard. The tool for that is StyleCop http://stylecop.codeplex.com/. My advice to you would be to install that along with ReSharper (this will fully enforce an excellent coding standard on you and your team) and then stop talking about coding standards for the rest of time :) – satnhak Dec 05 '12 at 11:25
  • I will try that, but is there any way we to have custom rules in Resharper ? – Sagar Dec 14 '12 at 16:08

1 Answers1

1

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.

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • thanks patrick I liked that, but as of now client do not have this tool so I have go ahead with fxcop customization only. Also there are few ways to identify the data type of control , so I can see that if that is the normal method or a delegate... – Sagar Dec 14 '12 at 16:09