diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 093a2fbe993..58a460483d7 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -778,19 +778,19 @@ private CommandInfo GetNextFunction() _commandName, WildcardOptions.IgnoreCase); - foreach (DictionaryEntry functionEntry in _context.EngineSessionState.GetFunctionTable()) + foreach ((string functionName, FunctionInfo functionInfo) in _context.EngineSessionState.GetFunctionTable()) { - if (functionMatcher.IsMatch((string)functionEntry.Key) || + if (functionMatcher.IsMatch(functionName) || (_commandResolutionOptions.HasFlag(SearchResolutionOptions.FuzzyMatch) && - FuzzyMatcher.IsFuzzyMatch(functionEntry.Key.ToString(), _commandName))) + FuzzyMatcher.IsFuzzyMatch(functionName, _commandName))) { - matchingFunction.Add((CommandInfo)functionEntry.Value); + matchingFunction.Add(functionInfo); } else if (_commandResolutionOptions.HasFlag(SearchResolutionOptions.UseAbbreviationExpansion)) { - if (_commandName.Equals(ModuleUtils.AbbreviateName((string)functionEntry.Key), StringComparison.OrdinalIgnoreCase)) + if (_commandName.Equals(ModuleUtils.AbbreviateName(functionName), StringComparison.OrdinalIgnoreCase)) { - matchingFunction.Add((CommandInfo)functionEntry.Value); + matchingFunction.Add(functionInfo); } } } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 709c84b973c..12472a11a6b 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -591,12 +591,12 @@ private PSObject GetSyntaxObject(CommandInfo command) if (this.Name != null && !Array.Exists(this.Name, name => name.Equals(command.Name, StringComparison.InvariantCultureIgnoreCase))) { string aliasName = _nameContainsWildcard ? command.Name : this.Name[0]; - + IDictionary aliasTable = SessionState.Internal.GetAliasTable(); foreach (KeyValuePair tableEntry in aliasTable) { - if ((Array.Exists(this.Name, name => name.Equals(tableEntry.Key, StringComparison.InvariantCultureIgnoreCase)) && - tableEntry.Value.Definition == command.Name) || + if ((Array.Exists(this.Name, name => name.Equals(tableEntry.Key, StringComparison.InvariantCultureIgnoreCase)) && + tableEntry.Value.Definition == command.Name) || (_nameContainsWildcard && tableEntry.Value.Definition == command.Name)) { aliasName = tableEntry.Key; @@ -635,7 +635,7 @@ private PSObject GetSyntaxObject(CommandInfo command) break; } - + syntax = PSObject.AsPSObject(replacedSyntax); } @@ -1444,15 +1444,13 @@ private IEnumerable GetMatchingCommandsFromModules(string commandNa // Look in function table if ((this.CommandType & (CommandTypes.Function | CommandTypes.Filter | CommandTypes.Configuration)) != 0) { - foreach (DictionaryEntry function in module.SessionState.Internal.GetFunctionTable()) + foreach ((string functionName, FunctionInfo functionInfo) in module.SessionState.Internal.GetFunctionTable()) { - FunctionInfo func = (FunctionInfo)function.Value; - - if (matcher.IsMatch((string)function.Key) && func.IsImported) + if (matcher.IsMatch(functionName) && functionInfo.IsImported) { // make sure function doesn't come from the current module's nested module - if (func.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) - yield return (CommandInfo)function.Value; + if (functionInfo.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) + yield return functionInfo; } } } diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index a4b30616132..11436624465 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -5074,29 +5074,29 @@ internal void RemoveModule(PSModuleInfo module, string moduleNameInRemoveModuleC // Remove the imported functions from SessionState... // (can't just go through module.SessionState.Internal.ExportedFunctions, // because the names of the functions might have been changed by the -Prefix parameter of Import-Module) - foreach (DictionaryEntry entry in ss.GetFunctionTable()) + foreach ((var _, FunctionInfo functionInfo) in ss.GetFunctionTable()) { - FunctionInfo func = (FunctionInfo)entry.Value; - if (func.Module == null) + if (functionInfo.Module == null) { continue; } - if (func.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) + if (functionInfo.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) { + string functionName = functionInfo.Name; try { - ss.RemoveFunction(func.Name, true); + ss.RemoveFunction(functionName, true); - string memberMessage = StringUtil.Format(Modules.RemovingImportedFunction, func.Name); + string memberMessage = StringUtil.Format(Modules.RemovingImportedFunction, functionName); WriteVerbose(memberMessage); } catch (SessionStateUnauthorizedAccessException e) { - string message = StringUtil.Format(Modules.UnableToRemoveModuleMember, func.Name, module.Name, e.Message); + string message = StringUtil.Format(Modules.UnableToRemoveModuleMember, functionName, module.Name, e.Message); InvalidOperationException memberNotRemoved = new InvalidOperationException(message, e); ErrorRecord er = new ErrorRecord(memberNotRemoved, "Modules_MemberNotRemoved", - ErrorCategory.PermissionDenied, func.Name); + ErrorCategory.PermissionDenied, functionName); WriteError(er); } } diff --git a/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs b/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs index b04e5e8d275..1b08aab2a2c 100644 --- a/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs @@ -39,7 +39,7 @@ internal void AddSessionStateEntry(SessionStateFunctionEntry entry) /// /// An IDictionary representing the visible functions. /// - internal IDictionary GetFunctionTable() + internal IDictionary GetFunctionTable() { SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); diff --git a/src/System.Management.Automation/namespaces/FunctionProvider.cs b/src/System.Management.Automation/namespaces/FunctionProvider.cs index d12808cf7c0..9de462c2e9e 100644 --- a/src/System.Management.Automation/namespaces/FunctionProvider.cs +++ b/src/System.Management.Automation/namespaces/FunctionProvider.cs @@ -309,7 +309,7 @@ internal override object GetValueOfItem(object item) /// internal override IDictionary GetSessionStateTable() { - return SessionState.Internal.GetFunctionTable(); + return (IDictionary)SessionState.Internal.GetFunctionTable(); } ///