0

I am looking to create .net code analysis tool based on custom rules. i am going to use reflection to read entire code. Is there any way to read number of line consumed in class or method using reflection or any other assembly.

I am thinking to use some opensource tool so i can do modification in it but unable to find it on Google.

Suggestions are welcome! thx

Maddy
  • 21
  • 4
  • Reflection is for compiled assemblies. Whats wrong with `File.ReadAllText()`? – Ash Burlaczenko Apr 23 '15 at 10:24
  • Reflection is used at runtime to inspect the compiled contents of a dll, not to inspect source code. You probably need to look into [Roslyn](https://github.com/dotnet/roslyn) as a starting point, but this question itself is far too broad for Stack Overflow. – James Thorpe Apr 23 '15 at 10:24
  • I used reflection to count the number of methods , constructor in a class. i can understand whole thing we cannot achieve using reflection. i am thinking to right code through scratch but it will need more time. :( – Maddy Apr 23 '15 at 10:42
  • 1
    Finding out anything about the source code requires reading the PDB file, just like the debugger does. That's possible, the DIA SDK is the usual choice. Maybe you are underestimating the effort to bring this to a good end. – Hans Passant Apr 23 '15 at 11:27
  • DIA sdk is not present in my system. m looking for its link to download but it is not available – Maddy Apr 23 '15 at 11:50

2 Answers2

1

You can't use reflection to "read the entire code". How will you get any single statement? Fundamentally you need a source code parser.

Maybe Roslyn is what you need.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Ira, I think Roslyn sdk is not available as i found it is in preview. its a built in feature under VS 2013+ versions . please confirm if m wrong? – Maddy Apr 23 '15 at 11:58
  • 1
    I don't know in fact about its availability; I've never fetched it personally. It is clear that many at SO have gotten some version (prerelease or not) and played with it, The fact that it is hosted at GitHub (see link) suggests you can reach right out and download it. Your alternative to "prerelease" is to go build your own stuff; Roslyn is so far beyond what you can reasonably dop in a year that you'd be crazy to do that. – Ira Baxter Apr 23 '15 at 14:13
0

Is there any way to read number of line consumed in class or method using reflection or any other assembly.

This is not possible. The best you could do is to inspect the MethodBody for each MethodInfo which

Provides access to the metadata and MSIL for the body of a method.

but lines of code is not something you can get. The number of lines is entirely a source code property and cannot be obtained through reflection.

Does

int x; x = 1; doSomethingWith(x);

count as one, two, or three lines? What about this:

int x;
x = 1;
doSomethingWith(x);

Lines of code is very rarely a useful metric - see, e.g. "When, if ever, is “number of lines of code” a useful metric?"


As Ira Baxter pointed out, you should probably look at Roslyn.

Community
  • 1
  • 1
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • Is there any Code parser available ?? which can parse whole .cs page in terms of class , method , properties . hope it clear :( – Maddy Apr 23 '15 at 10:56
  • Have a look at the [`System.CodeDom`](http://msdn.microsoft.com/en-us/library/gg145034) namespaces. I've not used CodeDom at all so have no idea, but it looks like it it *might* be useful. – Wai Ha Lee Apr 23 '15 at 11:07
  • System.codedom is useful in creating dynamic class . – Maddy Apr 23 '15 at 11:31
  • Oh, it looks like you're right. I was looking at [`CodeComProvider.Parse`](http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.parse) but actually that returns a [`CodeCompileUnit`](http://msdn.microsoft.com/en-us/library/system.codedom.codecompileunit) which doesn't help you. – Wai Ha Lee Apr 23 '15 at 11:46
  • i search about Microsoft.CCi but we cannot used it in our application as it comes with Fxcop only. i can do it manually also but it will not give the exact result. – Maddy Apr 23 '15 at 11:53