NDepend.API
Getting Started
Send Feedback

Glossary Item Box

Certainly the best way to get started with the NDepend.API infrastructure is to use the Power Tools  and browse their source code packed in the Visual Studio solution: $NDependInstallPath$\NDepend.PowerTools.SourceCode\NDepend.PowerTools.sln You can tweak existing power tools or create your own power tools.

Certainly the best way to get started with reading the code model from the NDepend.API, which types are in the namespace NDepend.CodeModel, is to browse default CQLinq code queries and code rules written on top of the code model.

The NDepend.API.dll assembly can be found in $NDependInstallPath$\Lib directory. You'll only need to reference this single assembly to use the NDepend.API.

The NDepend.API.dll assembly is an access point to most of NDepend features. Internally this assembly relies on most of others NDepend assemblies. As a consequence, using this assembly cannot be as easy as copy/pasting it. At the end of this documentation, see further explanations on How to build and deploy your program using NDepend.API.

 

The file $NDependInstallPath$\NDepend.PowerTools.SourceCode\NDepend_API_GettingStarted_Program.cs shows the basic code to get started with NDepend.API, here is the most relevant part. Types and methods linked are the backbone of NDepend.API:

 

// 0) Creates a NDependServicesProvider object

var ndependServicesProvider = new NDependServicesProvider();

 

 

// 1) obtain some VS solution or project file path

var visualStudioManager = ndependServicesProvider.VisualStudioManager;

ICollection<IAbsoluteFilePath> vsSlnOrProjFilePaths;

IntPtr ownerWindowHandle = ...; // get a non IntPtr.Zero owner Window Handle. If you are in a System.Console try this

visualStudioManager.ShowDialogSelectVisualStudioSolutionsOrProjects(ownerWindowHandle, out vsSlnOrProjFilePaths);

// Could also use:  visualStudioManager.GetMostRecentlyUsedVisualStudioSolutionOrProject() can also be used

 

 

// 2) obtains assemblies file path to analyze

var assembliesFilePath = (from vsSlnOrProjFilePath in vsSlnOrProjFilePaths

                           from assembliesFilePathTmp in visualStudioManager.GetAssembliesFromVisualStudioSolutionOrProject(vsSlnOrProjFilePath)

                           select assembliesFilePathTmp).Distinct().ToArray();

 

// 3) gets or create a IProject object

var projectManager = ndependServicesProvider.ProjectManager;

IProject project = projectManager.CreateTemporaryProject(assembliesFilePath, TemporaryProjectMode.Temporary);

// Or, to get a IProject object, could also use

//   projectManager.CreateBlankProject()  to create a persisten project

//   and then project.CodeToAnalyze.SetApplicationAssemblies()

//   and then projectManager.SaveProject(project); o save the project file

//

// Or, to get an existing IProject object, could also use

//    projectManager.ShowDialogChooseAnExistingProject(out project)

// Or programmatically list most recently used NDepend projects on this machine through

//    projectManager.GetMostRecentlyUsedProjects()

 

 

// 4) gets an IAnalysisResult object from the IProject object

IAnalysisResult analysisResult = project.RunAnalysis();  // *** This particular method works only with a Build Machine license ***

                                             //  Or  project.RunAnalysisAndBuildReport()  // *** This particular method works only with a Build Machine license ***

 

// Or, to get a IAnalysisResult object, first gets a IAnalysisResultRef object, that represents a reference to a persisted IAnalysisResult object

//    project.TryGetMostRecentAnalysisResultRef() or project.GetAvailableAnalysisResultsRefs() or project.GetAvailableAnalysisResultsRefsGroupedPerMonth()

// and then analysisResultRef.Load()

 

 

// 5) gets a ICodeBase object from the IAnalysisResult object

ICodeBase codeBase = analysisResult.CodeBase;

// Or eventually a ICompareContext object if you wish to analyze diff

// codeBase.CreateCompareContextWithOlder(olderCodeBase)

 

 

// 6) use the code model API to query code and do develop any algorithm you need!

// For example here we are looking for complex methods

var complexMethods = (from m in codeBase.Application.Methods

                        where m.ILCyclomaticComplexity > 10

                        orderby m.ILCyclomaticComplexity descending

                        select m).ToArray();

if (complexMethods.Length == 0) { return; }

Console.WriteLine("Press a key to show the " + complexMethods.Length + " most complex methods");

Console.ReadKey();

foreach (var m in complexMethods) {

   Console.WriteLine(m.FullName + " has a IL cyclomatic complexity of " + m.ILCyclomaticComplexity);

}

 

 

// 7) eventually lets the user opens source file declaration

if (complexMethods.First().SourceFileDeclAvailable) {

   var mostComplexMethod = complexMethods.First();

   Console.WriteLine("Press a key to open the source code decl of the most complex method?");

   Console.ReadKey();

   mostComplexMethod.TryOpenSource();

   // Eventually use ExtensionMethodsTooling.TryCompareSourceWith(NDepend.CodeModel.ISourceFileLine,NDepend.CodeModel.ISourceFileLine)

   // to compare 2 different versions of a code element

}

 

 

 


How to build and deploy your program using NDepend.API

To create your own program that references the assembly NDepend.API.dll a few steps are needed:

 

 

 

Copyright © 2004-2014 All rights reserved