-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When AppContext switch caching is disabled, default switch value was …
…not stored, we were lucky with switches that have "false" as a default
- Loading branch information
1 parent
fd9aec1
commit 18e6274
Showing
7 changed files
with
117 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/System.Windows.Forms/tests/TestUtilities/WinFormsAppContextSwitchScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Windows.Forms.Primitives; | ||
|
||
namespace System; | ||
|
||
/// <summary> | ||
/// Scope for temporarily setting an <see cref="AppContext"/> switch. Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// It is recommended to create wrappers for this struct for both simplicity and to allow adding synchronization. | ||
/// See <see cref="BinaryFormatterScope"/> for an example of doing this. | ||
/// </para> | ||
/// </remarks> | ||
public readonly ref struct WinFormsAppContextSwitchScope | ||
{ | ||
private readonly string _switchName; | ||
private readonly bool _originalState; | ||
|
||
public WinFormsAppContextSwitchScope(string switchName, bool enable) | ||
{ | ||
if (!AppContext.TryGetSwitch(AppContextSwitchNames.LocalAppContext_DisableCaching, out bool isEnabled) | ||
|| !isEnabled) | ||
{ | ||
// It doesn't make sense to try messing with AppContext switches if they are going to be cached. | ||
throw new InvalidOperationException("LocalAppContext switch caching is not disabled."); | ||
} | ||
|
||
if (!AppContext.TryGetSwitch(switchName, out _originalState)) | ||
{ | ||
var getDefaultValue = typeof(LocalAppContextSwitches).TestAccessor().CreateDelegate<Func<string, bool>>("GetSwitchDefaultValue"); | ||
_originalState = getDefaultValue(switchName); | ||
} | ||
|
||
AppContext.SetSwitch(switchName, enable); | ||
if (!AppContext.TryGetSwitch(switchName, out isEnabled) || isEnabled != enable) | ||
{ | ||
throw new InvalidOperationException($"Could not set {switchName} to {enable}."); | ||
} | ||
|
||
_switchName = switchName; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
AppContext.SetSwitch(_switchName, _originalState); | ||
if (!AppContext.TryGetSwitch(_switchName, out bool isEnabled) || isEnabled != _originalState) | ||
{ | ||
throw new InvalidOperationException($"Could not reset {_switchName} to {_originalState}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters