3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 using System.Collections.Generic;
11 public static class AppContext
14 private enum SwitchValueState
18 HasLookedForOverride = 0x4,
19 UnknownValue = 0x8 // Has no default and could not find an override
21 private static Dictionary<string, SwitchValueState> s_switchMap = new Dictionary<string, SwitchValueState>();
22 private static readonly object s_syncLock = new object();
24 public static string BaseDirectory
27 [System.Security.SecuritySafeCritical]
31 return AppDomain.CurrentDomain.BaseDirectory;
39 // populate the AppContext with the default set of values
40 AppContextDefaultValues.PopulateDefaultValues();
44 /// Try to get the value of the switch.
46 /// <param name="switchName">The name of the switch</param>
47 /// <param name="isEnabled">A variable where to place the value of the switch</param>
48 /// <returns>A return value of true represents that the switch was set and <paramref name="isEnabled"/> contains the value of the switch</returns>
49 public static bool TryGetSwitch(string switchName, out bool isEnabled)
51 if (switchName == null)
52 throw new ArgumentNullException("switchName");
53 if (switchName.Length == 0)
54 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
56 // By default, the switch is not enabled.
59 SwitchValueState switchValue;
62 if (s_switchMap.TryGetValue(switchName, out switchValue))
64 // The value is in the dictionary.
65 // There are 3 cases here:
66 // 1. The value of the switch is 'unknown'. This means that the switch name is not known to the system (either via defaults or checking overrides).
67 // Example: This is the case when, during a servicing event, a switch is added to System.Xml which ships before mscorlib. The value of the switch
68 // Will be unknown to mscorlib.dll and we want to prevent checking the overrides every time we check this switch
69 // 2. The switch has a valid value AND we have read the overrides for it
70 // Example: TryGetSwitch is called for a switch set via SetSwitch
71 // 3. The switch has the default value and we need to check for overrides
72 // Example: TryGetSwitch is called for the first time for a switch that has a default value
74 // 1. The value is unknown
75 if (switchValue == SwitchValueState.UnknownValue)
81 // We get the value of isEnabled from the value that we stored in the dictionary
82 isEnabled = (switchValue & SwitchValueState.HasTrueValue) == SwitchValueState.HasTrueValue;
84 // 2. The switch has a valid value AND we have checked for overrides
85 if ((switchValue & SwitchValueState.HasLookedForOverride) == SwitchValueState.HasLookedForOverride)
90 // 3. The switch has a valid value, but we need to check for overrides.
91 // Regardless of whether or not the switch has an override, we need to update the value to reflect
92 // the fact that we checked for overrides.
94 if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out overrideValue))
96 // we found an override!
97 isEnabled = overrideValue;
100 // Update the switch in the dictionary to mark it as 'checked for override'
101 s_switchMap[switchName] = (isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue)
102 | SwitchValueState.HasLookedForOverride;
108 // The value is NOT in the dictionary
109 // In this case we need to see if we have an override defined for the value.
110 // There are 2 cases:
111 // 1. The value has an override specified. In this case we need to add the value to the dictionary
112 // and mark it as checked for overrides
113 // Example: In a servicing event, System.Xml introduces a switch and an override is specified.
114 // The value is not found in mscorlib (as System.Xml ships independent of mscorlib)
115 // 2. The value does not have an override specified
116 // In this case, we want to capture the fact that we looked for a value and found nothing by adding
117 // an entry in the dictionary with the 'sentinel' value of 'SwitchValueState.UnknownValue'.
118 // Example: This will prevent us from trying to find overrides for values that we don't have in the dictionary
120 // 1. The value has an override specified.
122 if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out overrideValue))
124 isEnabled = overrideValue;
126 // Update the switch in the dictionary to mark it as 'checked for override'
127 s_switchMap[switchName] = (isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue)
128 | SwitchValueState.HasLookedForOverride;
133 // 2. The value does not have an override.
134 s_switchMap[switchName] = SwitchValueState.UnknownValue;
137 return false; // we did not find a value for the switch
141 /// Assign a switch a value
143 /// <param name="switchName">The name of the switch</param>
144 /// <param name="isEnabled">The value to assign</param>
145 public static void SetSwitch(string switchName, bool isEnabled)
147 if (switchName == null)
148 throw new ArgumentNullException("switchName");
149 if (switchName.Length == 0)
150 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
154 // Store the new value and the fact that we checked in the dictionary
155 s_switchMap[switchName] = (isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue)
156 | SwitchValueState.HasLookedForOverride;
161 /// This method is going to be called from the AppContextDefaultValues class when setting up the
162 /// default values for the switches. !!!! This method is called during the static constructor so it does not
163 /// take a lock !!!! If you are planning to use this outside of that, please ensure proper locking.
165 internal static void DefineSwitchDefault(string switchName, bool isEnabled)
167 s_switchMap[switchName] = isEnabled ? SwitchValueState.HasTrueValue : SwitchValueState.HasFalseValue;