1

I am curious to know how many static methods and static classes we have in our Visual Studio 2013 solution. We have lots of projects in there...

Is there anything built in to Visual Studio that can do this? I cant see anything in the Code Metrics window that countsthis.

I know I can use a Ctrl+F to search for static class (bit trickier using RegEx for static class). Also this is tricky for methods. So I get 350 or so static classes.

The only other thought I have on this is something like writing my own reflection based check.

Andez
  • 5,588
  • 20
  • 75
  • 116

2 Answers2

4

With ctrl-f and using "Look in: Entire Solution", you could just search with this regex for static methods

^\s*(public\s+|internal\s+|private\s+)?static\s+([a-zA-Z0-9_\<\>\.\:]+)\s+([a-zA-Z0-9_\<\>]+)\s*\(.*\)

and this for static classes

^\s*(public\s+|internal\s+|private\s+)?static\s+class

To be more confident of no false-positives you could limit the search to look only in *.cs files. At the bottom of "Find Results" you'll see "Matching lines:" with the count after it.

Limitations that I see:

  • A new line in the method or class declaration would break the search. This could be fixed by adding a (\r?\n)? after every token in the regexes above.
  • A block comment or an #ifdef 0 would not be detected. I don't think there's an easy way around this without parsing it more thoroughly with Roslyn (for example) or by compiling it and using reflection like you say in your question.
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
  • Hey John, yes indeed. The one thing is I like NDepend for this kind of thing, and all the fancy tools that get written that aid with development - especially on legacy systems. I will mark this as the answer as it is free! – Andez Apr 19 '16 at 18:40
1

The tool NDepend comes with a code query language based on C# LINQ + it integrates with VS2010,2012,2013,2015.

Concretely, to know how many static classes and static methods your projects have, you just need to edit two code queries:

from t in Application.Types where t.IsStatic select new { t, t.NbLinesOfCode }

and

from m in Application.Methods where m.IsStatic select new { m, m.NbLinesOfCode }

The queries is compiled live, and the matched code elements are listed.

Notice the code query language can be used to write code rule. Around 200 default code rules and queries are provided.

NDepend code query to list static methods

To obtain the percentage of static types you can write the query:

(Application.Types.Count(t => t.IsStatic) * 100f / Application.Types.Count())
.ToEnumerable().Sum()

To obtain the percentage of lines of code in static types you can write the query:

(Application.Types.Where(t => t.IsStatic).Sum(t => t.NbLinesOfCode) * 100f / Application.Types.Sum(t => t.NbLinesOfCode))
.ToEnumerable().Sum()

Disclaimer: I work for NDepend

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • 1
    I must admit I do like nDepend. I had the trial which has now expired. If I could get our dev manager to invest – Andez Feb 18 '16 at 09:12