使用反射计算代码行数的简单方法
我需要对 .net 程序集 (c#) 的代码行(无空白、无注释)进行(非常粗略的)估计。有没有一种简单的方法可以使用反射来做到这一点?
我更喜欢手写工具(所以我在这里询问指针......),但我也会使用免费(如语音)工具。
以下是我的用例:
我是一个团队的一员,负责一个不太大的项目,几乎没有代码覆盖率。我们有一份关于覆盖率的报告,其中指出覆盖率约为 60%(此处讨论的是单元测试)。但是该报告不显示根本没有单元测试的程序集。
因此,为了使报告接近正确,我想我编写了一个小工具,可以在没有单元测试的情况下为每个程序集调用该工具(我可以找到那些),并生成一个类似于我们的覆盖工具生成的 xml ,表明没有任何内容被覆盖。作为“语句”的第一个近似值,我认为我计算了“代码行”。
I need a (very rough) estimation on the lines of code (none-blank, none-comment) of an .net Assembly (c#). Is there an easy way to do this using reflection?
I would prefer a hand-written tool (so I am asking about pointers here...) but I would also settle with a free (as in speech) tool.
The following is my use-case:
I am part of a team, on a not-so-large project having virually no code coverage. We have a report on the coverage which states about 60% coveage (talking about unit-test here.) but this report does not display assemblies that have no unit-tests at all.
So to get the report anywhere near correct I thouhgt I write a small tool that could be called for every assembly without unit-tests (I can find those) and produce an xml like the one produced by our coverage-tool stating that nothing was coverd. As a first approximation for "statements" I thought I count "lines of code".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无法使用反射来计算代码行数。使用反射无法获得此信息。使用反射,您可以获得类成员的签名,并且可以获得这些方法内的原始 IL。然而,您无法有效地将 IL 转换回代码行。
有几种方法可以做到这一点。您可以(ab)使用 Reflector 等工具并以编程方式调用其程序集,将程序集反编译回 C# 并进行一些行计数,或者您可以从 .pdb 文件中获取信息以获取行号。这些程序数据库文件包含所有这些信息。然而,无法使用反射读取 pdb。
NDepend(Gerrie 提到的工具)使用 .pdb 文件中的信息来计算行数。
但是,由于您已经在使用代码覆盖工具,为什么不为未覆盖的程序集添加空的单元测试项目,并将这些测试项目添加到代码覆盖工具中。这样你就可以看到整个项目的总覆盖范围。这比购买 NDepend 便宜,也比自己手写 LoC 计数器便宜得多。
You can't 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 the raw IL inside those methods. There is however no way you can effectively translate that IL back to lines of code.
There are several ways to do this. You could (ab)use a tool such as Reflector and call its assemblies programmatically to decompile your assemblies back to C# and do some line counting, or you can harvest information from the .pdb files to get the line numbers. Those program database files contain all that information. There is however no way of reading pdbs using reflection.
NDepend (the tool Gerrie mentioned) uses information from the .pdb files to count number of lines.
But since you are already using a code coverage tool, why don't you add empty unit test projects for the uncovered assemblies and add those test projects to your code coverage tool. This way you can see the total coverage of the whole project. That would be cheaper than buying NDepend and much cheaper than handwriting a LoC counter yourself.
VS2010 使用 Metrics.exe (microsoft)。该工具计算已编译程序集中的代码行数。
http:// /blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx
VS2010 is using 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
使用像 NDepend 这样的工具而不是自己编写所有内容怎么样?
What about using a tool like NDepend instead of writing everything yourself?
好吧,如果您正在谈论已编译的程序集(如在 .dll 中),那么您无法轻松地从那里获取 LOC。您拥有的只是 IL 指令,单个 LOC 通常会生成多个 IL 指令。如果你想自己动手,你可能想看看Postsharp,它可以让你在IL中行走可以计算所有有趣的节点,但这仍然让您猜测如何计算 LOC。
另一个有趣的项目可能是 ILSpy,它有一个反编译器,可以(大约)重建 C# 源代码。不确定它是否可以编写脚本,但它是开源的,因此您可以根据需要扩展它。
您还可以尝试从 pdb 文件 中提取信息如果有的话。这可能是访问 PDB 的更简单方法< /a>
Well, if you are talking about compiled assemblies (as in .dll) then you can't easily get the LOCs from there. All you have is the IL instructions and a single LOC usually results in multiple IL instructions being generated. If you want to roll your own you probably want to have a look at Postsharp which lets you walk the IL and you can count all the interesting nodes but that still leaves you with the guess on how to calculate the LOCs.
Another interesting project might be ILSpy which has a decompiler and can reconstruct the C# source code (approximately). Not sure if it's scriptable however its open source so you can extend it to your needs.
You could also try to extract the information out of the pdb files if they are available. This might be an easier way of getting access to the PDBs
反射为您提供元数据而不是代码行。相反,加载程序集,找出类和模块,并给出统计数据,说明未涵盖以下方法/模块!
Reflection gives you meta data and not lines of code. Instead load the assemblies, find out the classes and modules and give out a statistics saying the following methods/modules are not covered !