| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace System |
| | | 7 | | { |
| | | 8 | | // Helper method for local caching of compatibility quirks. Keep this lean and simple - this file is included into |
| | | 9 | | // every framework assembly that implements any compatibility quirks. |
| | | 10 | | internal static partial class LocalAppContextSwitches |
| | | 11 | | { |
| | | 12 | | // Returns value of given switch using provided cache. |
| | | 13 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 14 | | internal static bool GetSwitchValue(string switchName, ref bool switchValue) => |
| | | 15 | | AppContext.TryGetSwitch(switchName, out switchValue); |
| | | 16 | | |
| | | 17 | | // Returns value of given switch using provided cache. |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | internal static bool GetCachedSwitchValue(string switchName, ref int cachedSwitchValue, bool defaultValue = fals |
| | | 20 | | { |
| | | 21 | | // The cached switch value has 3 states: 0 - unknown, 1 - true, -1 - false |
| | | 22 | | if (cachedSwitchValue < 0) return false; |
| | | 23 | | if (cachedSwitchValue > 0) return true; |
| | | 24 | | |
| | | 25 | | return GetCachedSwitchValueInternal(switchName, null, ref cachedSwitchValue, defaultValue); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | // Returns value of given switch or environment variable using provided cache. |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | internal static bool GetCachedSwitchValue(string switchName, string? envVariable, ref int cachedSwitchValue, boo |
| | 0 | 31 | | { |
| | | 32 | | // The cached switch value has 3 states: 0 - unknown, 1 - true, -1 - false |
| | 0 | 33 | | if (cachedSwitchValue < 0) return false; |
| | 0 | 34 | | if (cachedSwitchValue > 0) return true; |
| | | 35 | | |
| | 0 | 36 | | return GetCachedSwitchValueInternal(switchName, envVariable, ref cachedSwitchValue, defaultValue); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static bool GetCachedSwitchValueInternal(string switchName, string? envVariable, ref int cachedSwitchVal |
| | 0 | 40 | | { |
| | 0 | 41 | | if (!AppContext.TryGetSwitch(switchName, out bool isSwitchEnabled)) |
| | 0 | 42 | | { |
| | 0 | 43 | | if (envVariable == null || !TryGetBooleanEnvironmentVariable(envVariable, out isSwitchEnabled)) |
| | 0 | 44 | | { |
| | 0 | 45 | | isSwitchEnabled = defaultValue; |
| | 0 | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | AppContext.TryGetSwitch("TestSwitch.LocalAppContext.DisableCaching", out bool disableCaching); |
| | 0 | 50 | | if (!disableCaching) |
| | 0 | 51 | | { |
| | 0 | 52 | | cachedSwitchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/; |
| | 0 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | return isSwitchEnabled; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | private static bool TryGetBooleanEnvironmentVariable(string envVariable, out bool value) |
| | 0 | 59 | | { |
| | 0 | 60 | | if (Environment.GetEnvironmentVariable(envVariable) is string str) |
| | 0 | 61 | | { |
| | 0 | 62 | | if (str == "1" || string.Equals(str, bool.TrueString, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 63 | | { |
| | 0 | 64 | | value = true; |
| | 0 | 65 | | return true; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | if (str == "0" || string.Equals(str, bool.FalseString, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 69 | | { |
| | 0 | 70 | | value = false; |
| | 0 | 71 | | return true; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | value = false; |
| | 0 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | value = false; |
| | 0 | 79 | | return false; |
| | 0 | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |