When developing a tool for developers, at a point comes the need to propose an API to users to let consume the product features programmatically. NDepend.API has been released almost two years ago with NDepend v4.0. This API is pretty extensive and offers the possibility to achieve most common operations, including creating/loading a project, running an analysis, browsing the code model and all dependencies, metrics, attributes… and compiling/running CQLinq code queries and code rules.
When it comes to Continuous Integration, it is pretty convenient to run an analysis, run code rules and then report code rules violation, programmatically. While a lot of design effort have been spent on the API, I realized that using it to achieve these operations in sequentially might not be obvious at first glance. Hence here is the code excerpt for that:
You can download the VS solution here. You just need to unzip it in the $NDependInstallDir$\RulesRunner and then compile it to see RulesRunner.exe. Notice that since NDepend.API.dll is stored into $NDependInstallDir$\Lib, the source code uses the class AssemblyResolverHelper to redirect assembly resolving to the \Lib sub-dir.