DependencyScanner Class
Provides the base class for scanning source code files to discover dependencies across multiple package ecosystems and formats.
// Scan a directory for all dependencies
var dependencies = await DependencyScanner.ScanDirectoryAsync(
"C:\\MyProject",
options: null,
cancellationToken);
foreach (var dependency in dependencies)
{
Console.WriteLine($"{dependency.Type}: {dependency.Name}@{dependency.Version}");
}public abstract class DependencyScannerConstructors
| Name | Description |
|---|---|
| DependencyScanner() |
DependencyScanner()
protected DependencyScanner()No documentation is available.
Properties
| Name | Description |
|---|---|
| SupportedDependencyTypes |
SupportedDependencyTypes
protected internal abstract IReadOnlyCollection<DependencyType> SupportedDependencyTypes { get; }Property value
No documentation is available.
Methods
| Name | Description |
|---|---|
| ScanAsync(ScanFileContext) | When overridden in a derived class, performs the actual scanning of a file to discover dependencies. |
| ScanDirectoryAsync(string, ScannerOptions?, DependencyFound, CancellationToken) | Scans a directory and its subdirectories for dependencies, invoking a callback for each dependency found. |
| ScanDirectoryAsync(string, ScannerOptions?, CancellationToken) | Scans a directory and its subdirectories for dependencies. |
| ScanFileAsync(string, string, ScannerOptions?, CancellationToken) | Scans a single file from the file system for dependencies. |
| ScanFileAsync(string, string, byte[], IReadOnlyList<DependencyScanner>?, CancellationToken) | Scans a single file from memory for dependencies, using the specified scanners. |
| ScanFileAsync(string, string, byte[], CancellationToken) | Scans a single file from memory for dependencies. |
| ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, DependencyFound, CancellationToken) | Scans multiple specific files for dependencies, invoking a callback for each dependency found. |
| ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, CancellationToken) | Scans multiple specific files for dependencies. |
| ShouldScanFile(CandidateFileContext) | Determines whether this scanner should scan the specified file. |
| ShouldScanFile(ReadOnlySpan<char>, ReadOnlySpan<char>) | Determines whether this scanner should scan the specified file. |
| ShouldScanFileCore(CandidateFileContext) | When overridden in a derived class, determines whether this scanner should scan the specified file. |
ScanAsync(ScanFileContext)
When overridden in a derived class, performs the actual scanning of a file to discover dependencies.
public abstract ValueTask ScanAsync(ScanFileContext context)Parameters
contextScanFileContext- The context containing the file to scan and methods to report discovered dependencies.
Returns
ScanDirectoryAsync overloads
ScanDirectoryAsync(string, ScannerOptions?, DependencyFound, CancellationToken)
DependencyScanner.csScans a directory and its subdirectories for dependencies, invoking a callback for each dependency found.
net10.0
public static Task ScanDirectoryAsync(string path, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)net11.0
public static Task ScanDirectoryAsync(string path, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)Parameters
pathstring- The root directory path to scan.
optionsScannerOptions?- The scanner options, or
nullto use defaults. onDependencyFoundDependencyFound- The callback invoked when a dependency is found. It can be invoked concurrently.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Exceptions
DirectoryNotFoundException- The directory does not exist. The exception is reported through the returned task.
NotSupportedExceptionScannerOptions.FileSystemis a custom file system, and a file or directory predicate is set. The exception is reported through the returned task.
Remarks
Files are scanned concurrently unless ScannerOptions.DegreeOfParallelism is 1, so onDependencyFound can be invoked concurrently from several threads and must be thread-safe. An exception thrown by onDependencyFound stops the scan.
A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed. Symbolic links to directories are not followed, and symbolic links to files outside path are not scanned.
ScanDirectoryAsync(string, ScannerOptions?, CancellationToken)
DependencyScanner.csScans a directory and its subdirectories for dependencies.
net10.0
public static Task<IReadOnlyCollection<Dependency>> ScanDirectoryAsync(string path, ScannerOptions? options, CancellationToken cancellationToken = default)net11.0
public static Task<IReadOnlyCollection<Dependency>> ScanDirectoryAsync(string path, ScannerOptions? options, CancellationToken cancellationToken = default)Parameters
pathstring- The root directory path to scan.
optionsScannerOptions?- The scanner options, or
nullto use defaults. cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<IReadOnlyCollection<Dependency>> A collection of all discovered dependencies.
Exceptions
DirectoryNotFoundException- The directory does not exist. The exception is reported through the returned task.
NotSupportedExceptionScannerOptions.FileSystemis a custom file system, and a file or directory predicate is set. The exception is reported through the returned task.
Remarks
A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed. Symbolic links to directories are not followed, and symbolic links to files outside path are not scanned.
ScanFileAsync overloads
ScanFileAsync(string, string, ScannerOptions?, CancellationToken)
DependencyScanner.csScans a single file from the file system for dependencies.
net10.0
public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, ScannerOptions? options, CancellationToken cancellationToken = default)net11.0
public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, ScannerOptions? options, CancellationToken cancellationToken = default)Parameters
rootDirectorystring- The root directory context for relative path resolution.
filePathstring- The path of the file to scan.
optionsScannerOptions?- The scanner options, or
nullto use defaults. cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.
Remarks
A single file is handled like a file of a directory scan: when the file cannot be read, or a scanner fails to scan it, the failure is reported to ScannerOptions.OnFileScanFailed, the remaining scanners are skipped, and the dependencies found before the failure are returned.
ScanFileAsync(string, string, byte[], IReadOnlyList<DependencyScanner>?, CancellationToken)
DependencyScanner.csScans a single file from memory for dependencies, using the specified scanners.
net10.0
public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, IReadOnlyList<DependencyScanner>? scanners, CancellationToken cancellationToken = default)net11.0
public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, IReadOnlyList<DependencyScanner>? scanners, CancellationToken cancellationToken = default)Parameters
rootDirectorystring- The root directory context for relative path resolution.
filePathstring- The path of the file being scanned.
contentbyte[]- The file content as a byte array.
scannersIReadOnlyList<DependencyScanner>?- The scanners to use, or
nullto use the default scanners. cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.
Remarks
When a scanner fails to scan the content, the dependencies found before the failure are returned, and the failure is not reported. The locations of the dependencies are not updatable, because there is no file to write.
ScanFileAsync(string, string, byte[], CancellationToken)
DependencyScanner.csScans a single file from memory for dependencies.
public static Task<IReadOnlyCollection<Dependency>> ScanFileAsync(string rootDirectory, string filePath, byte[] content, CancellationToken cancellationToken = default)Parameters
rootDirectorystring- The root directory context for relative path resolution.
filePathstring- The path of the file being scanned.
contentbyte[]- The file content as a byte array.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<IReadOnlyCollection<Dependency>> A collection of dependencies found in the file.
Remarks
When a scanner fails to scan the content, the dependencies found before the failure are returned, and the failure is not reported. The locations of the dependencies are not updatable, because there is no file to write.
ScanFilesAsync overloads
ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, DependencyFound, CancellationToken)
DependencyScanner.csScans multiple specific files for dependencies, invoking a callback for each dependency found.
public static Task ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, DependencyFound onDependencyFound, CancellationToken cancellationToken = default)Parameters
rootDirectorystring- The root directory context for relative path resolution.
filePathsIEnumerable<string>- The collection of file paths to scan.
optionsScannerOptions?- The scanner options, or
nullto use defaults. onDependencyFoundDependencyFound- The callback invoked when a dependency is found. It can be invoked concurrently.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Remarks
Files are scanned concurrently unless ScannerOptions.DegreeOfParallelism is 1, so onDependencyFound can be invoked concurrently from several threads and must be thread-safe. An exception thrown by onDependencyFound stops the scan. A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed.
ScanFilesAsync(string, IEnumerable<string>, ScannerOptions?, CancellationToken)
DependencyScanner.csScans multiple specific files for dependencies.
net10.0
public static Task<IReadOnlyCollection<Dependency>> ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, CancellationToken cancellationToken = default)net11.0
public static Task<IReadOnlyCollection<Dependency>> ScanFilesAsync(string rootDirectory, IEnumerable<string> filePaths, ScannerOptions? options, CancellationToken cancellationToken = default)Parameters
rootDirectorystring- The root directory context for relative path resolution.
filePathsIEnumerable<string>- The collection of file paths to scan.
optionsScannerOptions?- The scanner options, or
nullto use defaults. cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<IReadOnlyCollection<Dependency>> A collection of all dependencies found across the specified files.
Remarks
A file that cannot be read, or that a scanner fails to scan, is skipped and reported to ScannerOptions.OnFileScanFailed.
ShouldScanFile overloads
ShouldScanFile(CandidateFileContext)
DependencyScanner.csDetermines whether this scanner should scan the specified file.
public bool ShouldScanFile(CandidateFileContext context)Parameters
contextCandidateFileContext- The context containing information about the candidate file.
Returns
bool true if the file should be scanned; otherwise, false.
ShouldScanFile(ReadOnlySpan<char>, ReadOnlySpan<char>)
DependencyScanner.csDetermines whether this scanner should scan the specified file.
public bool ShouldScanFile(ReadOnlySpan<char> rootDirectory, ReadOnlySpan<char> fullPath)Parameters
rootDirectoryReadOnlySpan<char>- The root directory path.
fullPathReadOnlySpan<char>- The full path of the file.
Returns
bool true if the file should be scanned; otherwise, false.
ShouldScanFileCore(CandidateFileContext)
When overridden in a derived class, determines whether this scanner should scan the specified file.
protected abstract bool ShouldScanFileCore(CandidateFileContext context)Parameters
contextCandidateFileContext- The context containing information about the candidate file.
Returns
bool true if the file should be scanned; otherwise, false.