Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -450,29 +450,26 @@ internal void RemoveOneSessionObjectFromCache(CimSession session)
{
DebugHelper.WriteLogEx();

if (!this.curCimSessionWrapper.ContainsKey(session))
if (curCimSessionWrapper.TryGetValue(session, out CimSessionWrapper wrapper))
{
return;
}
string name = wrapper.Name;
string computerName = wrapper.ComputerName;

CimSessionWrapper wrapper = this.curCimSessionWrapper[session];
string name = wrapper.Name;
string computerName = wrapper.ComputerName;
DebugHelper.WriteLog("name {0}, computername {1}, id {2}, instanceId {3}", 1, name, computerName, wrapper.SessionId, wrapper.InstanceId);

DebugHelper.WriteLog("name {0}, computername {1}, id {2}, instanceId {3}", 1, name, computerName, wrapper.SessionId, wrapper.InstanceId);
HashSet<CimSessionWrapper> objects;
if (this.curCimSessionsByComputerName.TryGetValue(computerName, out objects))
{
objects.Remove(wrapper);
}

HashSet<CimSessionWrapper> objects;
if (this.curCimSessionsByComputerName.TryGetValue(computerName, out objects))
{
objects.Remove(wrapper);
}
if (this.curCimSessionsByName.TryGetValue(name, out objects))
{
objects.Remove(wrapper);
}

if (this.curCimSessionsByName.TryGetValue(name, out objects))
{
objects.Remove(wrapper);
RemoveSessionInternal(session, wrapper);
}

RemoveSessionInternal(session, wrapper);
}

/// <summary>
Expand Down Expand Up @@ -533,7 +530,7 @@ internal IEnumerable<PSObject> QuerySession(
// NOTES: use template function to implement this will save duplicate code
foreach (uint id in ids)
{
if (this.curCimSessionsById.ContainsKey(id))
if (curCimSessionsById.TryGetValue(id, out CimSessionWrapper wrapper))
{
if (sessionIds.Add(id))
{
Expand Down Expand Up @@ -564,9 +561,8 @@ internal IEnumerable<PSObject> QuerySession(
errorRecords = errRecords;
foreach (Guid instanceid in instanceIds)
{
if (this.curCimSessionsByInstanceId.ContainsKey(instanceid))
if (curCimSessionsByInstanceId.TryGetValue(instanceid, out CimSessionWrapper wrapper))
{
CimSessionWrapper wrapper = this.curCimSessionsByInstanceId[instanceid];
if (!sessionIds.Contains(wrapper.SessionId))
{
sessionIds.Add(wrapper.SessionId);
Expand Down Expand Up @@ -640,9 +636,8 @@ internal IEnumerable<PSObject> QuerySessionByComputerName(
foreach (string computername in computernameArray)
{
bool foundSession = false;
if (this.curCimSessionsByComputerName.ContainsKey(computername))
if (curCimSessionsByComputerName.TryGetValue(computername, out HashSet<CimSessionWrapper> wrappers))
{
HashSet<CimSessionWrapper> wrappers = this.curCimSessionsByComputerName[computername];
foundSession = wrappers.Count > 0;
foreach (CimSessionWrapper wrapper in wrappers)
{
Expand Down Expand Up @@ -677,9 +672,8 @@ internal IEnumerable<PSObject> QuerySession(IEnumerable<CimSession> cimsessions,
errorRecords = errRecords;
foreach (CimSession cimsession in cimsessions)
{
if (this.curCimSessionWrapper.ContainsKey(cimsession))
if (curCimSessionWrapper.TryGetValue(cimsession, out CimSessionWrapper wrapper))
{
CimSessionWrapper wrapper = this.curCimSessionWrapper[cimsession];
if (!sessionIds.Contains(wrapper.SessionId))
{
sessionIds.Add(wrapper.SessionId);
Expand Down Expand Up @@ -714,13 +708,9 @@ internal CimSessionWrapper QuerySession(CimSession cimsession)
/// <returns>CimSession object.</returns>
internal CimSession QuerySession(Guid cimSessionInstanceId)
{
if (this.curCimSessionsByInstanceId.ContainsKey(cimSessionInstanceId))
{
CimSessionWrapper wrapper = this.curCimSessionsByInstanceId[cimSessionInstanceId];
return wrapper.CimSession;
}

return null;
return curCimSessionsByInstanceId.TryGetValue(cimSessionInstanceId, out CimSessionWrapper wrapper)
? wrapper.CimSession
: null;
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ internal static void AddCimSessionToTemporaryCache(CimSession session)
{
lock (temporarySessionCacheLock)
{
if (temporarySessionCache.ContainsKey(session))
if (temporarySessionCache.TryGetValue(session, out uint refCount))
{
temporarySessionCache[session]++;
DebugHelper.WriteLogEx(@"Increase cimsession ref count {0}", 1, temporarySessionCache[session]);
temporarySessionCache[session] = ++refCount;
DebugHelper.WriteLogEx(@"Increase cimsession ref count {0}", 1, refCount);
}
else
{
Expand All @@ -253,11 +253,11 @@ private static void RemoveCimSessionFromTemporaryCache(CimSession session,
bool removed = false;
lock (temporarySessionCacheLock)
{
if (temporarySessionCache.ContainsKey(session))
if (temporarySessionCache.TryGetValue(session, out uint refCount))
{
temporarySessionCache[session]--;
DebugHelper.WriteLogEx(@"Decrease cimsession ref count {0}", 1, temporarySessionCache[session]);
if (temporarySessionCache[session] == 0)
temporarySessionCache[session] = --refCount;
DebugHelper.WriteLogEx(@"Decrease cimsession ref count {0}", 1, refCount);
if (refCount == 0)
{
removed = true;
temporarySessionCache.Remove(session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public SwitchParameter Force
/// Encoding optional flag.
/// </summary>
[Parameter]
[ArgumentToEncodingTransformationAttribute]
[ArgumentToEncodingTransformationAttribute()]
[ArgumentEncodingCompletionsAttribute]
[ValidateNotNullOrEmpty]
public Encoding Encoding
Expand Down Expand Up @@ -2461,10 +2461,9 @@ private string GenerateReimportingOfModules()
{
StringBuilder result = new();

if (_invocationInfo.BoundParameters.ContainsKey(nameof(Module)))
if (_invocationInfo.BoundParameters.TryGetValue(nameof(Module), out object moduleNames))
{
string[] moduleNames = (string[])_invocationInfo.BoundParameters[nameof(Module)];
foreach (string moduleName in moduleNames)
foreach (string moduleName in (string[])moduleNames)
{
result.AppendFormat(
CultureInfo.InvariantCulture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ private bool TryGetRunspace(Breakpoint breakpoint)
Debug.Assert(runspaceInstanceIdProperty.TypeNameOfValue.Equals("System.Guid", StringComparison.OrdinalIgnoreCase), "Instance ids must be GUIDs.");

var runspaceInstanceId = (Guid)runspaceInstanceIdProperty.Value;
if (runspaces.ContainsKey(runspaceInstanceId))
if (runspaces.TryGetValue(runspaceInstanceId, out Runspace runspace))
{
Runspace = runspaces[runspaceInstanceId];
Runspace = runspace;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,20 +700,17 @@ protected override void ProcessRecord()
_cancelToken?.Dispose();
_cancelToken = null;
}
}

if (_followRelLink)
{
if (!_relationLink.ContainsKey("next"))
{
return;
}

uri = new Uri(_relationLink["next"]);
followedRelLink++;
}
if (!_followRelLink || !_relationLink.TryGetValue("next", out string value))
{
break;
}

uri = new Uri(value);

}
while (_followRelLink && (followedRelLink < _maximumFollowRelLink));
while (++followedRelLink < _maximumFollowRelLink);
}
catch (CryptographicException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,10 @@ internal void ResetTracing(Collection<PSTraceSource> matchingSources)
listener.Flush();
}

if (_storedTraceSourceState.ContainsKey(source))
if (_storedTraceSourceState.TryGetValue(source, out KeyValuePair<PSTraceSourceOptions, Collection<TraceListener>> storedState))
{
// Restore the TraceSource to its original state

KeyValuePair<PSTraceSourceOptions, Collection<TraceListener>> storedState =
_storedTraceSourceState[source];

source.Listeners.Clear();
foreach (TraceListener listener in storedState.Value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,9 @@ private Dictionary<string, ParameterMetadata> GetCommonParameters()

foreach (ParameterMetadata parameter in commonParameters.Values)
{
if ((parameter.ParameterSets.Count == 1) && (parameter.ParameterSets.ContainsKey(ParameterAttribute.AllParameterSets)))
if ((parameter.ParameterSets.Count == 1)
&& parameter.ParameterSets.TryGetValue(ParameterAttribute.AllParameterSets, out ParameterSetMetadata oldParameterSetMetadata))
{
ParameterSetMetadata oldParameterSetMetadata = parameter.ParameterSets[ParameterAttribute.AllParameterSets];

parameter.ParameterSets.Clear();
foreach (string parameterSetName in commonParameterSets)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,14 @@ private Dictionary<MergedCompiledCommandParameter, object> GetDefaultParameterVa

if (matches.Count == 1)
{
if (!availablePairs.ContainsKey(matches[0]))
if (!availablePairs.TryGetValue(matches[0], out object value))
{
availablePairs.Add(matches[0], wildcard.Value);
value = wildcard.Value;
availablePairs.Add(matches[0], value);
continue;
}

if (!wildcard.Value.Equals(availablePairs[matches[0]]))
if (!wildcard.Value.Equals(value))
{
if (!_warningSet.Contains(cmdletName + Separator + parameterName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2736,9 +2736,10 @@ private void ProcessCommandModifications(Runspace initializedRunspace)
private static void ProcessCommandModification(Hashtable commandModification, CommandMetadata metadata, string parameterName)
{
// If the metadata doesn't actually contain the parameter, then we need to create one.
if (!metadata.Parameters.ContainsKey(parameterName))
if (!metadata.Parameters.TryGetValue(parameterName, out ParameterMetadata value))
{
metadata.Parameters[parameterName] = new ParameterMetadata(parameterName);
value = new ParameterMetadata(parameterName);
metadata.Parameters[parameterName] = value;
}

// Add validation attributes
Expand All @@ -2752,13 +2753,13 @@ private static void ProcessCommandModification(Hashtable commandModification, Co
{
case "ValidateSet":
ValidateSetAttribute validateSet = new ValidateSetAttribute(parameterValidationValues);
metadata.Parameters[parameterName].Attributes.Add(validateSet);
value.Attributes.Add(validateSet);
break;

case "ValidatePattern":
string pattern = "^(" + string.Join('|', parameterValidationValues) + ")$";
ValidatePatternAttribute validatePattern = new ValidatePatternAttribute(pattern);
metadata.Parameters[parameterName].Attributes.Add(validatePattern);
value.Attributes.Add(validatePattern);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1279,16 +1279,14 @@ private void NewProviderEntry(ProviderInfo provider)
bool isDuplicateProvider = false;

// Add the entry to the list of providers with that name
if (!Providers.ContainsKey(provider.Name))
if (!Providers.TryGetValue(provider.Name, out List<ProviderInfo> existingProviders))
{
Providers.Add(provider.Name, new List<ProviderInfo>());
}
else
{
// be sure the same provider from the same PSSnapin doesn't already exist

List<ProviderInfo> existingProviders = Providers[provider.Name];

foreach (ProviderInfo existingProvider in existingProviders)
{
// making sure that we are not trying to add the same provider by checking the provider name & type of the new and existing providers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,16 +882,17 @@ internal void ProcessException(string moduleName, string culture, Exception e)
e.Message, ErrorCategory.InvalidOperation, null, e);
}

if (!_exceptions.ContainsKey(except.FullyQualifiedErrorId))
if (!_exceptions.TryGetValue(except.FullyQualifiedErrorId, out UpdatableHelpExceptionContext exceptionContext))
{
_exceptions.Add(except.FullyQualifiedErrorId, new UpdatableHelpExceptionContext(except));
exceptionContext = new UpdatableHelpExceptionContext(except);
_exceptions.Add(except.FullyQualifiedErrorId, exceptionContext);
}

_exceptions[except.FullyQualifiedErrorId].Modules.Add(moduleName);
exceptionContext.Modules.Add(moduleName);

if (culture != null)
{
_exceptions[except.FullyQualifiedErrorId].Cultures.Add(culture);
exceptionContext.Cultures.Add(culture);
}
}

Expand Down