3 // Copyright (c) Microsoft Corporation. All rights reserved.
10 using System.Runtime.CompilerServices;
12 internal static class AppContextSwitches
15 public static readonly bool ThrowExceptionIfDisposedCancellationTokenSource = false;
16 public static readonly bool SetActorAsReferenceWhenCopyingClaimsIdentity = false;
17 public static readonly bool NoAsyncCurrentCulture = false;
19 private static int _noAsyncCurrentCulture;
20 public static bool NoAsyncCurrentCulture
22 [MethodImpl(MethodImplOptions.AggressiveInlining)]
25 return GetCachedSwitchValue(AppContextDefaultValues.SwitchNoAsyncCurrentCulture, ref _noAsyncCurrentCulture);
29 private static int _throwExceptionIfDisposedCancellationTokenSource;
30 public static bool ThrowExceptionIfDisposedCancellationTokenSource
32 [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 return GetCachedSwitchValue(AppContextDefaultValues.SwitchThrowExceptionIfDisposedCancellationTokenSource, ref _throwExceptionIfDisposedCancellationTokenSource);
39 private static int _preserveEventListnerObjectIdentity;
40 public static bool PreserveEventListnerObjectIdentity
42 [MethodImpl(MethodImplOptions.AggressiveInlining)]
45 return GetCachedSwitchValue(AppContextDefaultValues.SwitchPreserveEventListnerObjectIdentity, ref _preserveEventListnerObjectIdentity);
49 private static int _useLegacyPathHandling;
52 /// Use legacy path normalization logic and blocking of extended syntax.
54 public static bool UseLegacyPathHandling
56 [MethodImpl(MethodImplOptions.AggressiveInlining)]
59 return GetCachedSwitchValue(AppContextDefaultValues.SwitchUseLegacyPathHandling, ref _useLegacyPathHandling);
63 private static int _blockLongPaths;
66 /// Throw PathTooLongException for paths greater than MAX_PATH or directories greater than 248 (as per CreateDirectory Win32 limitations)
68 public static bool BlockLongPaths
70 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 return GetCachedSwitchValue(AppContextDefaultValues.SwitchBlockLongPaths, ref _blockLongPaths);
77 private static int _cloneActor;
80 /// When copying a ClaimsIdentity.Actor this switch controls whether ClaimsIdentity.Actor should be set as a reference or the result of Actor.Clone()
82 public static bool SetActorAsReferenceWhenCopyingClaimsIdentity
84 [MethodImpl(MethodImplOptions.AggressiveInlining)]
87 return GetCachedSwitchValue(AppContextDefaultValues.SwitchSetActorAsReferenceWhenCopyingClaimsIdentity, ref _cloneActor);
91 private static int _doNotAddrOfCspParentWindowHandle;
92 public static bool DoNotAddrOfCspParentWindowHandle
94 [MethodImpl(MethodImplOptions.AggressiveInlining)]
97 return GetCachedSwitchValue(AppContextDefaultValues.SwitchDoNotAddrOfCspParentWindowHandle, ref _doNotAddrOfCspParentWindowHandle);
102 // Implementation details
105 private static bool DisableCaching { get; set; }
107 static AppContextSwitches()
110 if (AppContext.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out isEnabled))
112 DisableCaching = isEnabled;
116 [MethodImpl(MethodImplOptions.AggressiveInlining)]
117 internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
119 if (switchValue < 0) return false;
120 if (switchValue > 0) return true;
122 return GetCachedSwitchValueInternal(switchName, ref switchValue);
125 private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
127 bool isSwitchEnabled;
128 AppContext.TryGetSwitch(switchName, out isSwitchEnabled);
132 return isSwitchEnabled;
135 switchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
136 return isSwitchEnabled;