/// <summary> /// Private implementation that removes the ambient scope known to be the current one. /// Must only be called with the current, non-null scope. /// </summary> private static void RemoveAmbientScope(AmbientScope <TConcreteScope> ambientScope) { System.Diagnostics.Debug.Assert(ambientScope != null); System.Diagnostics.Debug.Assert(ambientScope == CurrentAmbientScope.Value); ReplaceAmbientScope(ambientScope, ambientScope.PhysicalParentScope); }
/// <summary> /// <para> /// Replaces the current ambient scope by the given new ambient scope or null. /// </para> /// <para> /// Throws if the current ambient scope is incorrect or the new ambient scope is already the current scope. /// </para> /// </summary> /// <param name="currentScope">The scope that is currently the ambient scope. Must be correct. May be null.</param> /// <param name="newAmbientScope">The scope that is to be set as the ambient scope. May be null.</param> protected static void ReplaceAmbientScope(AmbientScope <TConcreteScope> currentScope, AmbientScope <TConcreteScope>?newAmbientScope) { if (!ReferenceEquals(currentScope, CurrentAmbientScope.Value)) { throw new InvalidOperationException("The supposed current scope was not the current ambient scope. Always dispose or deactivate in reverse order of creation."); } SetAmbientScope(newAmbientScope); }
/// <summary> /// Makes the given scope the ambient one. /// Throws if it already is. /// </summary> /// <param name="newAmbientScope">The scope that is to be set as the ambient scope. May be null.</param> protected static void SetAmbientScope(AmbientScope <TConcreteScope>?newAmbientScope) { if (newAmbientScope is null) { CurrentAmbientScope.Value = null; return; } if (ReferenceEquals(newAmbientScope, CurrentAmbientScope.Value)) { throw new InvalidOperationException("The given scope was already the current ambient scope."); } CurrentAmbientScope.Value = (TConcreteScope)newAmbientScope; }