From 1b3b359bcbab32b9a3e7cd7ecb26642487cbb374 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sat, 21 Jan 2023 17:32:26 +0100 Subject: [PATCH] Use interpolated strings 1 (#18977) --- .../CimSessionOperations.cs | 2 +- .../CimSessionProxy.cs | 10 +++++----- .../RegisterCimIndicationCommand.cs | 2 +- .../Utils.cs | 8 +++----- .../HelpWindow/HelpParagraphBuilder.cs | 12 ++++++------ .../FilterCore/ValidatingSelectorValue.cs | 2 +- .../FilterCore/ValidatingValueBase.cs | 2 +- .../FilterProviders/SearchTextParser.cs | 4 ++-- .../ManagementList/ManagementList/Innerlist.cs | 2 +- .../ManagementList/ViewGroupToStringConverter.cs | 2 +- .../Controls/MultipleSelectionControl.xaml.cs | 2 +- .../Controls/ParameterSetControl.xaml.cs | 13 +++++-------- .../ShowCommand/ViewModel/CommandViewModel.cs | 2 +- .../ShowCommand/ViewModel/ParameterSetViewModel.cs | 4 ++-- .../ShowCommand/ViewModel/ParameterViewModel.cs | 2 +- .../PSVersionInfoGenerator.csproj | 1 + 16 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionOperations.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionOperations.cs index c22db2087e..f31e703520 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionOperations.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionOperations.cs @@ -951,7 +951,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets CimTestCimSessionContext testCimSessionContext = context as CimTestCimSessionContext; uint sessionId = this.sessionState.GenerateSessionId(); string originalSessionName = testCimSessionContext.CimSessionWrapper.Name; - string sessionName = originalSessionName ?? string.Format(CultureInfo.CurrentUICulture, @"{0}{1}", CimSessionState.CimSessionClassName, sessionId); + string sessionName = originalSessionName ?? string.Create(CultureInfo.CurrentUICulture, $"{CimSessionState.CimSessionClassName}{sessionId}"); // detach CimSession from the proxy object CimSession createdCimSession = testCimSessionContext.Proxy.Detach(); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs index bc69a4f5ce..64f5634f97 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs @@ -875,7 +875,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets parameters.Append(','); } - parameters.Append(string.Format(CultureInfo.CurrentUICulture, @"'{0}' = {1}", key, parameterList[key])); + parameters.Append(CultureInfo.CurrentUICulture, $@"'{key}' = {parameterList[key]}"); } } @@ -1232,7 +1232,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets this.operationParameters.Add(@"className", className); this.WriteOperationStartMessage(this.operationName, this.operationParameters); CimAsyncMultipleResults asyncResult = this.CimSession.EnumerateInstancesAsync(namespaceName, className, this.OperationOptions); - string errorSource = string.Format(CultureInfo.CurrentUICulture, "{0}:{1}", namespaceName, className); + string errorSource = string.Create(CultureInfo.CurrentUICulture, $"{namespaceName}:{className}"); ConsumeCimInstanceAsync(asyncResult, new CimResultContext(errorSource)); } @@ -1314,7 +1314,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets this.operationParameters.Add(@"className", className); this.WriteOperationStartMessage(this.operationName, this.operationParameters); CimAsyncMultipleResults asyncResult = this.CimSession.EnumerateClassesAsync(namespaceName, className, this.OperationOptions); - string errorSource = string.Format(CultureInfo.CurrentUICulture, "{0}:{1}", namespaceName, className); + string errorSource = string.Create(CultureInfo.CurrentUICulture, $"{namespaceName}:{className}"); ConsumeCimClassAsync(asyncResult, new CimResultContext(errorSource)); } @@ -1334,7 +1334,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets this.operationParameters.Add(@"className", className); this.WriteOperationStartMessage(this.operationName, this.operationParameters); CimAsyncResult asyncResult = this.CimSession.GetClassAsync(namespaceName, className, this.OperationOptions); - string errorSource = string.Format(CultureInfo.CurrentUICulture, "{0}:{1}", namespaceName, className); + string errorSource = string.Create(CultureInfo.CurrentUICulture, $"{namespaceName}:{className}"); ConsumeCimClassAsync(asyncResult, new CimResultContext(errorSource)); } @@ -1388,7 +1388,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets this.operationParameters.Add(@"methodName", methodName); this.WriteOperationStartMessage(this.operationName, this.operationParameters); CimAsyncMultipleResults asyncResult = this.CimSession.InvokeMethodAsync(namespaceName, className, methodName, methodParameters, this.OperationOptions); - string errorSource = string.Format(CultureInfo.CurrentUICulture, "{0}:{1}", namespaceName, className); + string errorSource = string.Create(CultureInfo.CurrentUICulture, $"{namespaceName}:{className}"); ConsumeCimInvokeMethodResultAsync(asyncResult, className, methodName, new CimResultContext(errorSource)); } diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs index d5dc44a31d..b314691e41 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs @@ -205,7 +205,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets case CimBaseCommand.ClassNameComputerSet: // validate the classname this.CheckArgument(); - tempQueryExpression = string.Format(CultureInfo.CurrentCulture, "Select * from {0}", this.ClassName); + tempQueryExpression = string.Create(CultureInfo.CurrentCulture, $"Select * from {this.ClassName}"); break; } diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs index 61afc00a67..be517c9864 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs @@ -201,16 +201,14 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets StackFrame frame = trace.GetFrame(depth); // if (withFileName) // { - // return string.Format(CultureInfo.CurrentUICulture, "{0}#{1}:{2}:", frame.GetFileName()., frame.GetFileLineNumber(), frame.GetMethod().Name); + // return string.Create(CultureInfo.CurrentUICulture, $"{frame.GetFileName().}#{frame.GetFileLineNumber()}:{frame.GetMethod().Name}:"); // } // else // { - // return string.Format(CultureInfo.CurrentUICulture, "{0}:", frame.GetMethod()); + // return string.Create(CultureInfo.CurrentUICulture, $"{frame.GetMethod()}:"); // } - return string.Format(CultureInfo.CurrentUICulture, "{0}::{1} ", - frame.GetMethod().DeclaringType.Name, - frame.GetMethod().Name); + return string.Create(CultureInfo.CurrentUICulture, $"{frame.GetMethod().DeclaringType.Name}::{frame.GetMethod().Name} "); } #endregion diff --git a/src/Microsoft.Management.UI.Internal/HelpWindow/HelpParagraphBuilder.cs b/src/Microsoft.Management.UI.Internal/HelpWindow/HelpParagraphBuilder.cs index a0f2cf950b..107ff67dd0 100644 --- a/src/Microsoft.Management.UI.Internal/HelpWindow/HelpParagraphBuilder.cs +++ b/src/Microsoft.Management.UI.Internal/HelpWindow/HelpParagraphBuilder.cs @@ -276,7 +276,7 @@ namespace Microsoft.Management.UI.Internal foreach (string line in lines) { // Indentation is not localized - returnValue.AppendFormat("{0}{1}\r\n", indentString, line); + returnValue.Append($"{indentString}{line}\r\n"); } if (returnValue.Length > 2) @@ -369,7 +369,7 @@ namespace Microsoft.Management.UI.Internal continue; } - string commandStart = string.Format(CultureInfo.CurrentCulture, "{0} ", commandName); + string commandStart = string.Create(CultureInfo.CurrentCulture, $"{commandName} "); this.AddText(HelpParagraphBuilder.AddIndent(commandStart), false); foreach (object parameterObj in parameterObjs) @@ -389,7 +389,7 @@ namespace Microsoft.Management.UI.Internal continue; } - string parameterType = parameterValue == null ? string.Empty : string.Format(CultureInfo.CurrentCulture, "<{0}>", parameterValue); + string parameterType = parameterValue == null ? string.Empty : string.Create(CultureInfo.CurrentCulture, $"<{parameterValue}>"); string parameterOptionalOpenBrace, parameterOptionalCloseBrace; @@ -607,7 +607,7 @@ namespace Microsoft.Management.UI.Internal description = GetPropertyString(propertyTypeObject, "description"); } - memberText = string.Format(CultureInfo.CurrentCulture, " [{0}] {1}\r\n", propertyType, name); + memberText = string.Create(CultureInfo.CurrentCulture, $" [{propertyType}] {name}\r\n"); } } else if (string.Equals("method", type, StringComparison.OrdinalIgnoreCase)) @@ -697,7 +697,7 @@ namespace Microsoft.Management.UI.Internal } } - string paramString = string.Format(CultureInfo.CurrentCulture, "[{0}] ${1},", parameterType, parameterName); + string paramString = string.Create(CultureInfo.CurrentCulture, $"[{parameterType}] ${parameterName},"); parameterText.Append(paramString); } @@ -709,7 +709,7 @@ namespace Microsoft.Management.UI.Internal } } - memberText = string.Format(CultureInfo.CurrentCulture, " [{0}] {1}({2})\r\n", returnType, name, parameterText); + memberText = string.Create(CultureInfo.CurrentCulture, $" [{returnType}] {name}({parameterText})\r\n"); } /// diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingSelectorValue.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingSelectorValue.cs index e86f2020ec..9b72d5848e 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingSelectorValue.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingSelectorValue.cs @@ -188,7 +188,7 @@ namespace Microsoft.Management.UI.Internal { if (!columnName.Equals(SelectedIndexPropertyName, StringComparison.CurrentCulture)) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "{0} is not a valid column name.", columnName), "columnName"); + throw new ArgumentException(string.Create(CultureInfo.CurrentCulture, $"{columnName} is not a valid column name."), "columnName"); } if (!this.IsIndexWithinBounds(this.SelectedIndex)) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs index f33862846a..3a07218027 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterCore/ValidatingValueBase.cs @@ -218,7 +218,7 @@ namespace Microsoft.Management.UI.Internal DataErrorInfoValidationResult result = rule.Validate(value, cultureInfo); if (result == null) { - throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "DataErrorInfoValidationResult not returned by ValidationRule: {0}", rule.ToString())); + throw new InvalidOperationException(string.Create(CultureInfo.CurrentCulture, $"DataErrorInfoValidationResult not returned by ValidationRule: {rule}")); } if (!result.IsValid) diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/SearchTextParser.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/SearchTextParser.cs index c64683c730..8900964fba 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/SearchTextParser.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/SearchTextParser.cs @@ -121,7 +121,7 @@ namespace Microsoft.Management.UI.Internal patterns.Add(rule.Pattern); } - patterns.Add(string.Format(CultureInfo.InvariantCulture, "(?<{0}>){1}", FullTextRuleGroupName, ValuePattern)); + patterns.Add(string.Create(CultureInfo.InvariantCulture, $"(?<{FullTextRuleGroupName}>){ValuePattern}")); return string.Join("|", patterns.ToArray()); } @@ -199,7 +199,7 @@ namespace Microsoft.Management.UI.Internal this.UniqueId = uniqueId; this.selectorFilterRule = selectorFilterRule; this.childRule = childRule; - this.Pattern = string.Format(CultureInfo.InvariantCulture, "(?<{0}>){1}\\s*:\\s*{2}", uniqueId, Regex.Escape(selectorFilterRule.DisplayName), SearchTextParser.ValuePattern); + this.Pattern = string.Create(CultureInfo.InvariantCulture, $"(?<{uniqueId}>){Regex.Escape(selectorFilterRule.DisplayName)}\\s*:\\s*{SearchTextParser.ValuePattern}"); } /// diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/Innerlist.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/Innerlist.cs index fe9e0fdcd1..a970a5290e 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/Innerlist.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/Innerlist.cs @@ -596,7 +596,7 @@ namespace Microsoft.Management.UI.Internal propertyValue = string.Empty; } - entryText.AppendFormat(CultureInfo.CurrentCulture, "{0}\t", propertyValue); + entryText.Append(CultureInfo.CurrentCulture, $"{propertyValue}\t"); } return entryText.ToString(); diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/ViewGroupToStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/ViewGroupToStringConverter.cs index c69cd8e0c0..22741a7031 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/ViewGroupToStringConverter.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/ViewGroupToStringConverter.cs @@ -35,7 +35,7 @@ namespace Microsoft.Management.UI.Internal } string name = (!string.IsNullOrEmpty(cvg.Name.ToString())) ? cvg.Name.ToString() : UICultureResources.GroupTitleNone; - string display = string.Format(CultureInfo.CurrentCulture, "{0} ({1})", name, cvg.ItemCount); + string display = string.Create(CultureInfo.CurrentCulture, $"{name} ({cvg.ItemCount})"); return display; } diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/MultipleSelectionControl.xaml.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/MultipleSelectionControl.xaml.cs index 54dcf1896f..f57d5dfda5 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/MultipleSelectionControl.xaml.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/MultipleSelectionControl.xaml.cs @@ -43,7 +43,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal foreach (object selectedItem in multipleSelectionDialog.listboxParameter.SelectedItems) { - newComboText.AppendFormat(CultureInfo.InvariantCulture, "{0},", selectedItem.ToString()); + newComboText.Append(CultureInfo.InvariantCulture, $"{selectedItem},"); } if (newComboText.Length > 1) diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/ParameterSetControl.xaml.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/ParameterSetControl.xaml.cs index 08f9df2933..907c743f6b 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/ParameterSetControl.xaml.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/Controls/ParameterSetControl.xaml.cs @@ -94,7 +94,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal //// Add AutomationProperties.AutomationId for Ui Automation test. checkBox.SetValue( System.Windows.Automation.AutomationProperties.AutomationIdProperty, - string.Format(CultureInfo.CurrentCulture, "chk{0}", parameterViewModel.Name)); + string.Create(CultureInfo.CurrentCulture, $"chk{parameterViewModel.Name}")); checkBox.SetValue( System.Windows.Automation.AutomationProperties.NameProperty, @@ -124,10 +124,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal Binding selectedItemBinding = new Binding("Value"); comboBox.SetBinding(ComboBox.SelectedItemProperty, selectedItemBinding); - string automationId = string.Format( - CultureInfo.CurrentCulture, - "combox{0}", - parameterViewModel.Name); + string automationId = string.Create(CultureInfo.CurrentCulture, $"combox{parameterViewModel.Name}"); //// Add AutomationProperties.AutomationId for Ui Automation test. comboBox.SetValue( @@ -164,7 +161,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal multiControls.comboxParameter.SetBinding(ComboBox.TextProperty, valueBinding); // Add AutomationProperties.AutomationId for Ui Automation test. - multiControls.SetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty, string.Format("combox{0}", parameterViewModel.Name)); + multiControls.SetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty, string.Create(CultureInfo.CurrentCulture, $"combox{parameterViewModel.Name}")); multiControls.comboxParameter.SetValue( System.Windows.Automation.AutomationProperties.NameProperty, @@ -206,7 +203,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal //// Add AutomationProperties.AutomationId for UI Automation test. textBox.SetValue( System.Windows.Automation.AutomationProperties.AutomationIdProperty, - string.Format(CultureInfo.CurrentCulture, "txt{0}", parameterViewModel.Name)); + string.Create(CultureInfo.CurrentCulture, $"txt{parameterViewModel.Name}")); textBox.SetValue( System.Windows.Automation.AutomationProperties.NameProperty, @@ -397,7 +394,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal //// Add AutomationProperties.AutomationId for Ui Automation test. label.SetValue( System.Windows.Automation.AutomationProperties.AutomationIdProperty, - string.Format(CultureInfo.CurrentCulture, "lbl{0}", parameterViewModel.Name)); + string.Create(CultureInfo.CurrentCulture, $"lbl{parameterViewModel.Name}")); return label; } diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/CommandViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/CommandViewModel.cs index 367b5d0813..835ae6d3c9 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/CommandViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/CommandViewModel.cs @@ -431,7 +431,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal if (commandName.Contains(' ')) { - builder.AppendFormat("& \"{0}\"", commandName); + builder.Append($"& \"{commandName}\""); } else { diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterSetViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterSetViewModel.cs index 9d59ca3dc1..2b70deff7d 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterSetViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterSetViewModel.cs @@ -138,7 +138,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal { if (((bool?)parameter.Value) == true) { - builder.AppendFormat("-{0} ", parameter.Name); + builder.Append($"-{parameter.Name} "); } continue; @@ -166,7 +166,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal parameterValueString = ParameterSetViewModel.GetDelimitedParameter(parameterValueString, "(", ")"); } - builder.AppendFormat("-{0} {1} ", parameter.Name, parameterValueString); + builder.Append($"-{parameter.Name} {parameterValueString} "); } return builder.ToString().Trim(); diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterViewModel.cs index 32eb827193..fd5b14fff1 100644 --- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterViewModel.cs +++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/ParameterViewModel.cs @@ -159,7 +159,7 @@ namespace Microsoft.PowerShell.Commands.ShowCommandInternal string returnValue = this.Parameter.Name; if (this.Parameter.IsMandatory) { - returnValue = string.Format(CultureInfo.CurrentUICulture, "{0}{1}", returnValue, ShowCommandResources.MandatoryLabelSegment); + returnValue = string.Create(CultureInfo.CurrentUICulture, $"{returnValue}{ShowCommandResources.MandatoryLabelSegment}"); } return returnValue; diff --git a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj index 8ebdc3a61a..c312121359 100644 --- a/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj +++ b/src/System.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj @@ -9,6 +9,7 @@ netstandard2.0 10.0 true + true