/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Collections; using System.Collections.Generic; namespace System.Management.Automation { internal sealed class SessionStateScopeEnumerator : IEnumerator, IEnumerable { /// /// Constructs an enumerator for enumerating through the session state scopes /// using the appropriate scoping rules (default to dynamic scoping). /// /// /// /// The starting scope to start the enumeration from. /// internal SessionStateScopeEnumerator(SessionStateScope scope) { Diagnostics.Assert(scope != null, "Caller to verify scope argument"); _initialScope = scope; } // ctor /// /// Uses the proper scoping rules to get the next scope to do the lookup in. /// /// /// /// True if the enumerator was advanced to the next scope, or false otherwise. /// /// public bool MoveNext() { // On the first call to MoveNext the enumerator should be before // the first scope in the lookup and then advance to the first // scope in the lookup _currentEnumeratedScope = _currentEnumeratedScope == null ? _initialScope : _currentEnumeratedScope.Parent; // If the current scope is the global scope there is nowhere else // to do the lookup, so return false. return (_currentEnumeratedScope != null); } // MoveNext /// /// Sets the enumerator to before the first scope /// public void Reset() { _currentEnumeratedScope = null; } // Reset /// /// Gets the current lookup scope /// /// /// /// The enumerator is positioned before the first element of the /// collection or after the last element. /// /// SessionStateScope IEnumerator.Current { get { if (_currentEnumeratedScope == null) { throw PSTraceSource.NewInvalidOperationException(); } return _currentEnumeratedScope; } // get } // Current object IEnumerator.Current { get { return ((IEnumerator)this).Current; } } /// /// Gets the IEnumerator for this class /// /// /// /// The IEnumerator interface for this class. /// System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator() { return this; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this; } public void Dispose() { Reset(); } private readonly SessionStateScope _initialScope; private SessionStateScope _currentEnumeratedScope; } // class SessionStateScopeEnumerator } // namespace System.Management.Automation