An easy way to count lines of code using reflection

I need a (very rough) grade in lines of code (no spaces, no comments) .net Assembly (C #). Is there an easy way to do this with reflection?

I would prefer a handwritten tool (so I ask about pointers here ...), but I would also agree with a free (as in speech) tool.

The following is an example of use:
I am part of a team, in a not-so-large project, where there is no code coverage. We have a coverage report that talks about 60% coveage (we are talking about the unit test here.) , But this report does not show assemblies that do not have unit tests at all.
Therefore, to get a report somewhere near the right one, I will write a small tool that could be called for each assembly without unit tests (I can find them) and create an xml similar to the one that was created by our coverage tool, in which It says that nothing was closed. In a first approximation for "statements" I thought I was counting "lines of code."

+4
source share
5 answers

You cannot count lines of code using reflection. This information is not available using reflection. Using reflection, you can get the signature of class members, and you can get raw IL inside these methods. However, you cannot effectively translate this IL back into lines of code.

There are several ways to do this. You can (ab) use a tool like Reflector and programmatically call its assemblies to decompile assemblies in C # and perform line counting, or you can collect information from .pdb files to get line numbers. These program database files contain all this information. However, there is no way to read pdbs using reflection.

NDepend (the mentioned Gerrie tool) uses information from .pdb files to count the number of lines.

But since you already use the code coverage tool, why don't you add empty unit test projects for uncovered assemblies and add these test projects to your code coverage tool. Thus, you can see the overall coverage of the entire project. It would be cheaper than buying NDepend and much cheaper than actually writing a LoC counter.

+5
source

VS2010 uses Metrics.exe (microsoft). The tool counts lines of code from compiled assemblies. http://blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx

+4
source

How to use NDepend instead of having to write it all yourself?

+2
source

Well, if you are talking about prefabricated assemblies (like in .dll), you cannot easily get LOC from there. All you have is an IL instruction, and one LOC usually results in the generation of several IL instructions. If you want to collapse yourself, you probably want to see Postsharp , which allows you to walk along the IL, and you can read all the interesting nodes, but that still leaves you with the assumption of how to calculate the LOC.

Another interesting project might be ILSpy , which has a decompiler and can restore C # source code (approximately). Not sure if it can be open source, so you can extend it to your needs.

You can also try to extract information from pdb files , if available. This may be an easier way to access the PDB.

+1
source

Reflection gives you metadata, not lines of code. Instead, load the assemblies, find out the classes and modules, and produce statistics that do not cover the following methods / modules:

0
source

All Articles