diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs index 2f916ad172..3d45bb9664 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Properties/PlatformTargetBuildPropertyPageEnumProvider.cs @@ -3,44 +3,49 @@ using Microsoft.Build.Framework.XamlTypes; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Threading; -using EnumCollection = System.Collections.Generic.ICollection; namespace Microsoft.VisualStudio.ProjectSystem.Properties; /// -/// Responsible for producing valid values for the TargetPlatform property from a design-time build. +/// Responsible for producing valid values for the TargetPlatform MSBuild property. /// +/// +/// Candidate values from the AvailablePlatforms MSBuild property. +/// [ExportDynamicEnumValuesProvider("PlatformTargetEnumProvider")] [AppliesTo(ProjectCapability.DotNet)] -internal class PlatformTargetBuildPropertyPageEnumProvider : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator +[method: ImportingConstructor] +internal sealed class PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) : IDynamicEnumValuesProvider, IDynamicEnumValuesGenerator { - private const string AnyCpuPlatformName = "AnyCPU"; - private const string AnyCpuDisplayName = "Any CPU"; - - private readonly ProjectProperties _properties; - - [ImportingConstructor] - public PlatformTargetBuildPropertyPageEnumProvider(ProjectProperties properties) - { - _properties = properties; - } - public bool AllowCustomValues => false; - public async Task GetListedValuesAsync() + public async Task> GetListedValuesAsync() { - var result = new List(); - - ConfigurationGeneral configuration = await _properties.GetConfigurationGeneralPropertiesAsync(); + ConfigurationGeneral configuration = await properties.GetConfigurationGeneralPropertiesAsync(); string availablePlatformsTargets = await configuration.AvailablePlatforms.GetDisplayValueAsync(); + List enumValues = []; + HashSet targets = new(StringComparers.ConfigurationDimensionValues); + foreach (string platformTarget in new LazyStringSplit(availablePlatformsTargets, ',')) { - result.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget })); + // Prevent duplicates. + if (targets.Add(platformTarget)) + { + enumValues.Add(new PageEnumValue(new EnumValue() { Name = platformTarget, DisplayName = GetDisplayName(platformTarget) })); + } } - return result; + return enumValues; + + static string GetDisplayName(string platformTarget) + { + const string AnyCpuPlatformName = "AnyCPU"; + const string AnyCpuDisplayName = "Any CPU"; + + return platformTarget.Equals(AnyCpuPlatformName, StringComparisons.ConfigurationDimensionValues) ? AnyCpuDisplayName : platformTarget; + } } public Task GetProviderAsync(IList? options)