Juri
Juri

Reputation: 32920

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

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).

Upvotes: 2

Views: 1954

Answers (6)

Tom Carter
Tom Carter

Reputation: 2976

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)

Upvotes: 0

Ira Baxter
Ira Baxter

Reputation: 95392

See C# Metrics tool that computes CC and a wide variety of other metrics.

Upvotes: 0

chyne
chyne

Reputation: 667

Gendarme does some code metrics (sort of) with the help of Mono.Cecil, perhaps it would help with what you are trying to accomplish?

Upvotes: 3

driis
driis

Reputation: 164331

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.

Upvotes: 0

Bryan Watts
Bryan Watts

Reputation: 45465

Have a look at the Common Compiler Infrastructure (CCI) from Microsoft Research.

Upvotes: 1

Andy
Andy

Reputation: 30418

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.

Upvotes: 0

Related Questions