As an example, let's say that I need to check for a certain usage of C# code and warn the user that that is a bad pratice. There are multiple usages that I want to detect, such as:

  • Resource not being released by developer (in a heavy COM environment)
  • Instance should be created using Activator instead of new (I don't have control of the API I'm analyzing)
  • Others

The implementation of the API can be in any language, but I'll be mostly analyzing c# and vb.net.

Suitable languages:

  • C#
  • Python
  • Other
  • It's not really obvious whether you are looking to analyze any language, or just C#. – Steven Jeuris Jan 17 '12 at 13:45

No need to reinvent wheel. StyleCop already does this.

It also allows for user rules via extension API. For example detecting the Activator usage should be rather easy thing to do (however detecting legitimate usage of Activator may be very non-trivial task, but that would apply to any code analysis solution).

There is also ReSharper which (in addition to being extensible on its own in a similar way) can integrate StyleCop directly into Visual Studio, so the analysis can be done as the programmer types the code without having to manually launch a separate tool to check the conventions.

StyleCop can of course also be launched on its own via GUI or from command line (so integration into whatever build system/CI you are using is possible).

  • actually that is an interesting solution. no need to code and stylecop may give me the results I need. I'll investigate this further. – George Silva Jan 17 '12 at 13:18
  • 2
    I agree with the "Use existing tooling" sentiment, but I'm not sure StyleCop will catch the kind of issues he is looking to catch, it's more for coding standards than coding practices ( a fuzzy line indeed). Perhaps FxCop would be better. – Ed James Jan 17 '12 at 15:00
  • 1
    @EdWoodcock: Didn't FxCop get merged into the /analyze compiler option? – Ben Voigt Jan 18 '12 at 3:07
  • @BenVoigt Not as far as I am aware, it comes with the Windows SDK (see stackoverflow.com/a/3039160/70847). I think the /analyze option you refer to is for C/C++. – Ed James Jan 18 '12 at 10:12

Matěj Zábský's answer might be what you are looking for, but to answer your actual question you might take a look at the new Microsoft Roslyn project. It's a public API which allows analysis of C# code.

More importantly, we are opening up the C# and Visual Basic compilers and exposing all that rich information and code analysis to be available for your use. We expose a public API surface and provide extension points in the C# and VB language services.

This opens up new opportunities for VS extenders to write powerful refactorings and language analysis tools, as well as allow anyone to incorporate our parsers, semantic engines, code generators and scripting in their own applications.

  • wow, Roslyn seems to be real powerful! – George Silva Jan 17 '12 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.