Reputation: 21
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
Upvotes: 0
Views: 136
Reputation: 8815
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.
Upvotes: -1
Reputation: 95410
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.
Upvotes: 1