use IntPtr instead of CriticalHandle to avoid resurrection issues. It's ok to never...
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / LocalAppContextSwitches.Common.cs
blobe383092d06135eb56abd76b5aa797bd841a2d15f
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 // See the LICENSE file in the project root for more information.
5 using System.Runtime.CompilerServices;
7 namespace System
9 // Helper method for local caching of compatibility quirks. Keep this lean and simple - this file is included into
10 // every framework assembly that implements any compatibility quirks.
11 internal static partial class LocalAppContextSwitches
13 // Returns value of given switch using provided cache.
14 [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 internal static bool GetCachedSwitchValue(string switchName, ref int cachedSwitchValue)
17 // The cached switch value has 3 states: 0 - unknown, 1 - true, -1 - false
18 if (cachedSwitchValue < 0) return false;
19 if (cachedSwitchValue > 0) return true;
21 return GetCachedSwitchValueInternal(switchName, ref cachedSwitchValue);
24 private static bool GetCachedSwitchValueInternal(string switchName, ref int cachedSwitchValue)
26 bool isSwitchEnabled;
28 bool hasSwitch = AppContext.TryGetSwitch(switchName, out isSwitchEnabled);
29 if (!hasSwitch)
31 isSwitchEnabled = GetSwitchDefaultValue(switchName);
34 AppContext.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out bool disableCaching);
35 if (!disableCaching)
37 cachedSwitchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
40 return isSwitchEnabled;
43 // Provides default values for switches if they're not always false by default
44 private static bool GetSwitchDefaultValue(string switchName)
46 if (switchName == "Switch.System.Runtime.Serialization.SerializationGuard")
48 return true;
51 return false;