3 // Copyright (c) Microsoft Corporation. All rights reserved.
10 using System.Runtime.CompilerServices;
12 internal static class AppContextSwitches
14 private static int _noAsyncCurrentCulture;
15 public static bool NoAsyncCurrentCulture
17 [MethodImpl(MethodImplOptions.AggressiveInlining)]
20 return GetCachedSwitchValue(AppContextDefaultValues.SwitchNoAsyncCurrentCulture, ref _noAsyncCurrentCulture);
24 private static int _throwExceptionIfDisposedCancellationTokenSource;
25 public static bool ThrowExceptionIfDisposedCancellationTokenSource
27 [MethodImpl(MethodImplOptions.AggressiveInlining)]
30 return GetCachedSwitchValue(AppContextDefaultValues.SwitchThrowExceptionIfDisposedCancellationTokenSource, ref _throwExceptionIfDisposedCancellationTokenSource);
34 private static int _preserveEventListnerObjectIdentity;
35 public static bool PreserveEventListnerObjectIdentity
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
40 return GetCachedSwitchValue(AppContextDefaultValues.SwitchPreserveEventListnerObjectIdentity, ref _preserveEventListnerObjectIdentity);
44 private static int _useLegacyPathHandling;
47 /// Use legacy path normalization logic and blocking of extended syntax.
49 public static bool UseLegacyPathHandling
51 [MethodImpl(MethodImplOptions.AggressiveInlining)]
54 return GetCachedSwitchValue(AppContextDefaultValues.SwitchUseLegacyPathHandling, ref _useLegacyPathHandling);
58 private static int _blockLongPaths;
61 /// Throw PathTooLongException for paths greater than MAX_PATH or directories greater than 248 (as per CreateDirectory Win32 limitations)
63 public static bool BlockLongPaths
65 [MethodImpl(MethodImplOptions.AggressiveInlining)]
68 return GetCachedSwitchValue(AppContextDefaultValues.SwitchBlockLongPaths, ref _blockLongPaths);
72 private static int _cloneActor;
75 /// When copying a ClaimsIdentity.Actor this switch controls whether ClaimsIdentity.Actor should be set as a reference or the result of Actor.Clone()
77 public static bool SetActorAsReferenceWhenCopyingClaimsIdentity
79 [MethodImpl(MethodImplOptions.AggressiveInlining)]
82 return GetCachedSwitchValue(AppContextDefaultValues.SwitchSetActorAsReferenceWhenCopyingClaimsIdentity, ref _cloneActor);
87 // Implementation details
90 private static bool DisableCaching { get; set; }
92 static AppContextSwitches()
95 if (AppContext.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out isEnabled))
97 DisableCaching = isEnabled;
101 [MethodImpl(MethodImplOptions.AggressiveInlining)]
102 internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
104 if (switchValue < 0) return false;
105 if (switchValue > 0) return true;
107 return GetCachedSwitchValueInternal(switchName, ref switchValue);
110 private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
112 bool isSwitchEnabled;
113 AppContext.TryGetSwitch(switchName, out isSwitchEnabled);
117 return isSwitchEnabled;
120 switchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
121 return isSwitchEnabled;