Table of Contents

Class ExtensionMethodsHelpers

Namespace
NDepend.CodeModel
Assembly
NDepend.API.dll

Provides a set of extension methods that makes the code model API usage more convenient to use.

public static class ExtensionMethodsHelpers
Inheritance
ExtensionMethodsHelpers
Inherited Members

Methods (15)

ContainsSameCodeElementsThan<TCodeElement>(HashSet<TCodeElement>, HashSet<TCodeElement>)

Returns true if this codeElementsHashset hashset contains the same set of code elements than the otherCodeElementsHashset hashset. Otherwise returns false.

public static bool ContainsSameCodeElementsThan<TCodeElement>(this HashSet<TCodeElement> codeElementsHashset, HashSet<TCodeElement> otherCodeElementsHashset) where TCodeElement : class, ICodeElement

Parameters

codeElementsHashset HashSet<TCodeElement>

This hahshet of code elements objects.

otherCodeElementsHashset HashSet<TCodeElement>

The other hahshet of code elements objects.

Returns

bool

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This ContainsSameCodeElementsThan<TCodeElement>(IEnumerable<TCodeElement>, IEnumerable<TCodeElement>) overload, relies on the fact that both codeElementsHashset and otherCodeElementsHashset are hashsets, to execute with a faster O(codeElementsHashset.Count) time complexity.

ContainsSameCodeElementsThan<TCodeElement>(HashSet<TCodeElement>, IEnumerable<TCodeElement>)

Returns true if this codeElementsHashset hashset contains the same set of code elements than the otherCodeElements sequence. Otherwise returns false.

public static bool ContainsSameCodeElementsThan<TCodeElement>(this HashSet<TCodeElement> codeElementsHashset, IEnumerable<TCodeElement> otherCodeElements) where TCodeElement : class, ICodeElement

Parameters

codeElementsHashset HashSet<TCodeElement>

This hahshet of code elements objects.

otherCodeElements IEnumerable<TCodeElement>

The other sequence of code elements objects.

Returns

bool

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This ContainsSameCodeElementsThan<TCodeElement>(IEnumerable<TCodeElement>, IEnumerable<TCodeElement>) overload, relies on the fact that both codeElementsHashset and otherCodeElements are hashsets, to execute with a faster O(otherCodeElements.Count) time complexity.
Notice that otherCodeElements can contain doublons, hence this operation first gets otherCodeElements.Distinct().
codeElementsHashset necessarily contains distinct code elements.

ContainsSameCodeElementsThan<TCodeElement>(IEnumerable<TCodeElement>, HashSet<TCodeElement>)

Returns true if this codeElements sequence contains the same set of code elements than the otherCodeElementsHashset hashset. Otherwise returns false.

public static bool ContainsSameCodeElementsThan<TCodeElement>(this IEnumerable<TCodeElement> codeElements, HashSet<TCodeElement> otherCodeElementsHashset) where TCodeElement : class, ICodeElement

Parameters

codeElements IEnumerable<TCodeElement>

This sequence of code elements objects.

otherCodeElementsHashset HashSet<TCodeElement>

The hahshet of code elements objects.

Returns

bool

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This ContainsSameCodeElementsThan<TCodeElement>(IEnumerable<TCodeElement>, IEnumerable<TCodeElement>) overload, relies on the fact that otherCodeElementsHashset is a hashset to execute with a faster O(codeElements.Count) time complexity.
Notice that codeElements can contain doublons, hence this operation first gets codeElements.Distinct().
otherCodeElementsHashset necessarily contains distinct code elements.

ContainsSameCodeElementsThan<TCodeElement>(IEnumerable<TCodeElement>, IEnumerable<TCodeElement>)

Returns true if this codeElements sequence contains the same set of code elements than the otherCodeElements sequence. Otherwise returns false.

public static bool ContainsSameCodeElementsThan<TCodeElement>(this IEnumerable<TCodeElement> codeElements, IEnumerable<TCodeElement> otherCodeElements) where TCodeElement : class, ICodeElement

Parameters

codeElements IEnumerable<TCodeElement>

This sequence of code elements objects.

otherCodeElements IEnumerable<TCodeElement>

The other sequence of code elements objects.

Returns

bool

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

Notice that both sequences can contain doublons, hence this operation first gets codeElements.Distinct() and otherCodeElements.Distinct().
This extension method has a O(codeElements.Count + otherCodeElements.Count) time complexity.

CreateCompareContextWithOlder(ICodeBase, ICodeBase)

Creates a new ICompareContext object, with newer for NewerCodeBase and older for OlderCodeBase.

public static ICompareContext CreateCompareContextWithOlder(this ICodeBase newer, ICodeBase older)

Parameters

newer ICodeBase

The ICodeBase object that represents the NewerCodeBase in the created compare context.

older ICodeBase

The ICodeBase object that represents the OlderCodeBase in the created compare context.

Returns

ICompareContext

Remarks

newer can be equal to older. This possibility is convenient if you need a see ICompareContext object from a ICodeBase object.

See Also

ExceptTestCode<TCodeElement>(IEnumerable<TCodeElement>)

Filter code elements object from this codeElements sequence. Keeps only code elements for those ICodeElement.IsTestCode is false.

public static IEnumerable<TCodeElement> ExceptTestCode<TCodeElement>(this IEnumerable<TCodeElement> codeElements) where TCodeElement : class, ICodeElement

Parameters

codeElements IEnumerable<TCodeElement>

This sequence of code elements objects.

Returns

IEnumerable<TCodeElement>

A sequence of code elements objects with a false value for IsTestCode.

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This extension method has a O(codeElements.Count) time complexity.

See Also

ExceptThirdParty<TCodeElement>(IEnumerable<TCodeElement>)

Filter code elements object from this codeElements sequence. Keeps only code elements for those ICodeElement.IsThirdParty is false.

public static IEnumerable<TCodeElement> ExceptThirdParty<TCodeElement>(this IEnumerable<TCodeElement> codeElements) where TCodeElement : class, ICodeElement

Parameters

codeElements IEnumerable<TCodeElement>

This sequence of code elements objects.

Returns

IEnumerable<TCodeElement>

A sequence of code elements objects with a false value for IsThirdParty.

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This extension method has a O(codeElements.Count) time complexity.

See Also

FillIncremental<TCodeElement>(IEnumerable<TCodeElement>, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>)

Iteratively fills a sequence of code elements, until no new element can be added. The first iteration starts with initialSeq, and the function func is used to compute new elements of the iteration N+1 from all elements computed until the iteration N.

public static ICodeMetric<TCodeElement, ushort> FillIncremental<TCodeElement>(this IEnumerable<TCodeElement> initialSeq, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>> func) where TCodeElement : class, ICodeElement

Parameters

initialSeq IEnumerable<TCodeElement>

The initial sequence of code elements consumed by the first iteration.

func Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>

The function used to compute new elements of the iteration N+1 from all elements computed until the iteration N.

Returns

ICodeMetric<TCodeElement, ushort>

A new ICodeMetric<TCodeElement, TNumeric> object. The code metric value for each element in DefinitionDomain is the iteration number when it was added. Value for code elements in initialSeq is 0.

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This method differs from FillIterative<TCodeElement>(IEnumerable<TCodeElement>, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>) because all elements computed until iteration N are used to compute elements of iteration N+1.

Exceptions

InvalidOperationException

The number of iterations has outreached the maximum iterations threshold, which is fixed to 100.

ArgumentNullException

initialSeq or a sequence returned by a call to func contains a null TCodeElement.

FillIterative<TCodeElement>(IEnumerable<TCodeElement>, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>)

Iteratively fills a sequence of code elements, until no new element can be added. The first iteration starts with initialSeq, and the function func is used to compute new elements of the iteration N+1 from new elements computed at iteration N.

public static ICodeMetric<TCodeElement, ushort> FillIterative<TCodeElement>(this IEnumerable<TCodeElement> initialSeq, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>> func) where TCodeElement : class, ICodeElement

Parameters

initialSeq IEnumerable<TCodeElement>

The initial sequence of code elements consumed by the first iteration.

func Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>

The function used to compute new elements of the iteration N+1 from new elements computed at iteration N.

Returns

ICodeMetric<TCodeElement, ushort>

A new ICodeMetric<TCodeElement, TNumeric> object. The code metric value for each element in DefinitionDomain is the iteration number when it was added. Value for code elements in initialSeq is 0.

Type Parameters

TCodeElement

The code element type that is ICodeElement or that implements ICodeElement.

Remarks

This method differs from FillIncremental<TCodeElement>(IEnumerable<TCodeElement>, Func<IEnumerable<TCodeElement>, IEnumerable<TCodeElement>>) because only elements computed at iteration N are used to compute elements of iteration N+1.

Exceptions

InvalidOperationException

The number of iterations has outreached the maximum iterations threshold, which is fixed to 100.

ArgumentNullException

initialSeq or a sequence returned by a call to func contains a null TCodeElement.

GetImage<T>(ICodeElement)

Get the image that represents the codeElement.

public static T GetImage<T>(this ICodeElement codeElement) where T : class

Parameters

codeElement ICodeElement

The code element.

Returns

T

Type Parameters

T

GetNbLinesOfCode_GuaranteedIfPDBFound(ICodeElement)

Try gets a number of lines of code for an application codeElement. This value is guaranteed not being zero if the code element is in an assembly for which PDB file has been found and parsed.

public static uint? GetNbLinesOfCode_GuaranteedIfPDBFound(this ICodeElement codeElement)

Parameters

codeElement ICodeElement

The code element for which we want to obtain the number of line of code

Returns

uint?

Remarks

The metric ICodeContainer.NbLinesOfCode is computed from number of sequence points obtained at analysis time when parsing a PDB file.
As a consequence, if a PDB file is not found, all associated ICodeElement objects have a nullNbLinesOfCode.

s However there are several cases where this metric is not defined for a code elements declared in an assembly with PDB file found:

  • Abstract method
  • Implicit constructor
  • Field
  • Interface
  • Enumeration
  • Namespace, assembly or code base that contains only interfaces and enumerations

    This method is returning a non-null, non-zero value if codeElement satisfies the PDB found condition.
  • This value is 1 for any abstract method, implicit constructor or field.
  • In all other cases, codeElement is a ICodeElementParent object, and this methods returns NbLinesOfCode + MeAndRecursiveChildren.Count<TSource>(IEnumerable<TSource>).
    Notice that in the peculiar case where codeElementis a ICodeBase, the PDB found condition means that a PDB file must have been resolved for all application assemblies at analysis time.
    Notice that this method returns null for any third-party code element.
See Also

ToFullNameLikePredicate(string)

Returns a predicate object that takes a IMember object as input parameter, and returns a boolean that determines whether the member's FullName matches the regular expression regexString.

public static Predicate<ICodeElement> ToFullNameLikePredicate(this string regexString)

Parameters

regexString string

The regular expression string.

Returns

Predicate<ICodeElement>

Remarks

The regular expression is defined with the System.Text.RegularExpressions syntax.
The regular expression string can be suffixed with "\i" to specify the ignore case option.

Exceptions

ArgumentException

regexString is an invalid regular exception.

See Also
WithFullNameLike<TCodeElement>(IEnumerable<TCodeElement>, string)

ToNameLikePredicate(string)

Returns a predicate object that takes a ICodeElement object as input parameter, and returns a boolean that determines whether the code element's Name matches the regular expression regexString.

public static Predicate<ICodeElement> ToNameLikePredicate(this string regexString)

Parameters

regexString string

The regular expression string.

Returns

Predicate<ICodeElement>

Remarks

The regular expression is defined with the System.Text.RegularExpressions syntax.
The regular expression string can be suffixed with "\i" to specify the ignore case option.

Exceptions

ArgumentException

regexString is an invalid regular exception.

See Also
WithNameLike<TCodeElement>(IEnumerable<TCodeElement>, string)

ToSimpleNameLikePredicate(string)

Returns a predicate object that takes a IMethod object as input parameter, and returns a boolean that determines whether the code element's SimpleName matches the regular expression regexString.

public static Predicate<ICodeElement> ToSimpleNameLikePredicate(this string regexString)

Parameters

regexString string

The regular expression string.

Returns

Predicate<ICodeElement>

Remarks

The regular expression is defined with the System.Text.RegularExpressions syntax.
The regular expression string can be suffixed with "\i" to specify the ignore case option.

Exceptions

ArgumentException

regexString is an invalid regular exception.

See Also
WithSimpleNameLike<TCodeElement>(IEnumerable<TCodeElement>, string)

TryGetSourceFileContent(ICodeBase, IAbsoluteFilePath, out string, out string)

Try to get the content of a source file as it was at analysis time.

public static bool TryGetSourceFileContent(this ICodeBase codeBase, IAbsoluteFilePath sourceFilePath, out string sourceFileContent, out string failureReason)

Parameters

codeBase ICodeBase

The code base that contains the source file.

sourceFilePath IAbsoluteFilePath

The source file absolute path obtained from ICodeElement.SourceDecls or from ICodeBase.SourceFiles.

sourceFileContent string

If this method succeeds and returns true, contains source file content.

failureReason string

If this method fails and returns false, contains a plain-english description of the failure.

Returns

bool

Remarks

If ZipSourceFiles is true (which is the default value) source files get zipped at analysis time.
This method attempts to retrieve a source file content from this zipped store.
This method is different than trying to get the content from sourceFilePath. Since the analysis has been executed the source file might have been edited or removed.