简体   繁体   中英

Analyzing code structure using CodeDom?

I recently wrote a post here on Stackoverflow asking for some C# libraries that calculate metrics, mainly CC...unfortunately with no success. So I'm going to write it myself. I did a search on the web of what could be the best approach, but before starting I'd like to ask you on how you'd do it.

I'm currently between two kind of approaches

  • Given a source code directory, to parse the source code with regex expressions or similar for identifying the constructs like methods, conditional statements etc. for being able to calculate CC
  • Given an assembly, loading it and analyzing it (using CodeDom?)

I'm more for the 2nd approach, since parsing the source code directly doesn't seem to be a good approach to me. I've read about CodeDom which is integrated in the .Net framework. I know it is used for dynamic code generation. I guess I could also use it for analyzing the code structure, can't I? Does anybody of you have some good starting point of using CodeDom, some hints, good tutorials where to start?

Thanks

Edit: Or possibly some other utility that allows to parse source code easily (DOM like structure).

GendarmeMono.Cecil的帮助下进行了一些代码度量(也许),这可能对您要完成的工作有所帮助?

看看Microsoft Research的Common Compiler Infrastructure(CCI)

The problem with using CodeDom is that it is one way - there are APIs for generating code, but none for parsing code. I seem to recall hearing about some unsupported/hidden APIs that parse code into CodeDom structures, but I'm not sure.

Also, how would you get the code from the assembly itself? Reflection doesn't go down to the IL, but only to members of classes.

Have a look at this CodeProject article . It seems to be the beginning of what you are trying to do, but would need some additions; since the example code in the article does not parse members, only types and namespaces.

There exist no complete parsers for CodeDOM that I know of, which is also mentioned on the BCL Team Blog .

请参阅计算CC和其他指标的C#指标工具

Since you're concerned with .NET have a look at using .Net Reflector

There are a variety of Plugins one of which calculates some basic metrics including CC.

If you don't find what you're looking for why not write your own. .NET Reflector has a plugin API providing a type of CodeDom model that you can analyse easily (also see at bottom of page )

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