简体   繁体   中英

Building .Net Code Analysis Tool

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

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.

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, eg " When, if ever, is “number of lines of code” a useful metric? "


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM