dotnet/msbuild

[Feature Request]: Add property to `ProjectRootElement` for convenient access to `<Sdk>` elements

Open

#9,546 opened on Dec 14, 2023

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (5,062 stars) (1,364 forks)batch import
Feature RequestPriority:3backloghelp wantedtriaged

Description

Summary

Project files can contain <Sdk> elements (modeled by ProjectSdkElement). For parity with other project contents, there should be a property to access only those elements (e.g. SdkReferences).

Background and Motivation

There are already properties for accessing the Sdk attribute, all properties, all imports and all items. However, while Sdk elements appear in Children, they have no dedicated convenience accessor of their own.

Proposed Feature

/// <summary>
/// Get a read-only collection of the SDK references.
/// </summary>
public ICollection<ProjectSdkElement> Sdks => new ReadOnlyCollection<ProjectSdkElement>(Children.OfType<ProjectSdkElement>());

(name could also be SdkElements for better distinction from Sdk; there is also ChooseElements) (using Children and not AllChildren because I assume these elements are only valid at the top level)

A nice-to-have additional property (SdkReferences) or method (GetSdkReferences()) would be one that gathers all SDK references in a project, from all sources:

  • Sdk attribute on the project
  • Sdk elements
  • Import elements that specify an SDK (That would make it easy for something like Dependabot to check whether a particular project references an SDK package for which an update is available.)

Alternative Designs

No response

Contributor guide