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
;
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
)
28 bool hasSwitch
= AppContext
.TryGetSwitch(switchName
, out isSwitchEnabled
);
31 isSwitchEnabled
= GetSwitchDefaultValue(switchName
);
34 AppContext
.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out bool 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")