mscorlib(4.0.0.0) API with additions
AppContext.cs
2 
3 namespace System
4 {
6  public static class AppContext
7  {
8  [Flags]
9  private enum SwitchValueState
10  {
11  HasFalseValue = 0x1,
12  HasTrueValue = 0x2,
13  HasLookedForOverride = 0x4,
14  UnknownValue = 0x8
15  }
16 
17  private static readonly Dictionary<string, SwitchValueState> s_switchMap = new Dictionary<string, SwitchValueState>();
18 
19  private static volatile bool s_defaultsInitialized = false;
20 
23  public static string BaseDirectory => ((string)AppDomain.CurrentDomain.GetData("APP_CONTEXT_BASE_DIRECTORY")) ?? AppDomain.CurrentDomain.BaseDirectory;
24 
28 
32  public static object GetData(string name)
33  {
34  return AppDomain.CurrentDomain.GetData(name);
35  }
36 
37  private static void InitializeDefaultSwitchValues()
38  {
39  lock (s_switchMap)
40  {
41  if (!s_defaultsInitialized)
42  {
43  AppContextDefaultValues.PopulateDefaultValues();
44  s_defaultsInitialized = true;
45  }
46  }
47  }
48 
58  public static bool TryGetSwitch(string switchName, out bool isEnabled)
59  {
60  if (switchName == null)
61  {
62  throw new ArgumentNullException("switchName");
63  }
64  if (switchName.Length == 0)
65  {
66  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
67  }
68  if (!s_defaultsInitialized)
69  {
70  InitializeDefaultSwitchValues();
71  }
72  isEnabled = false;
73  lock (s_switchMap)
74  {
75  if (s_switchMap.TryGetValue(switchName, out SwitchValueState value))
76  {
77  if (value == SwitchValueState.UnknownValue)
78  {
79  isEnabled = false;
80  return false;
81  }
82  isEnabled = ((value & SwitchValueState.HasTrueValue) == SwitchValueState.HasTrueValue);
83  if ((value & SwitchValueState.HasLookedForOverride) == SwitchValueState.HasLookedForOverride)
84  {
85  return true;
86  }
87  if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out bool overrideValue))
88  {
89  isEnabled = overrideValue;
90  }
91  s_switchMap[switchName] = (SwitchValueState)(((!isEnabled) ? 1 : 2) | 4);
92  return true;
93  }
94  if (AppContextDefaultValues.TryGetSwitchOverride(switchName, out bool overrideValue2))
95  {
96  isEnabled = overrideValue2;
97  s_switchMap[switchName] = (SwitchValueState)(((!isEnabled) ? 1 : 2) | 4);
98  return true;
99  }
100  s_switchMap[switchName] = SwitchValueState.UnknownValue;
101  }
102  return false;
103  }
104 
112  public static void SetSwitch(string switchName, bool isEnabled)
113  {
114  if (switchName == null)
115  {
116  throw new ArgumentNullException("switchName");
117  }
118  if (switchName.Length == 0)
119  {
120  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "switchName");
121  }
122  if (!s_defaultsInitialized)
123  {
124  InitializeDefaultSwitchValues();
125  }
126  SwitchValueState value = (SwitchValueState)(((!isEnabled) ? 1 : 2) | 4);
127  lock (s_switchMap)
128  {
129  s_switchMap[switchName] = value;
130  }
131  }
132 
133  internal static void DefineSwitchDefault(string switchName, bool isEnabled)
134  {
135  s_switchMap[switchName] = ((!isEnabled) ? SwitchValueState.HasFalseValue : SwitchValueState.HasTrueValue);
136  }
137 
138  internal static void DefineSwitchOverride(string switchName, bool isEnabled)
139  {
140  s_switchMap[switchName] = (SwitchValueState)(((!isEnabled) ? 1 : 2) | 4);
141  }
142  }
143 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Provides members for setting and retrieving data about an application's context.
Definition: AppContext.cs:6
string TargetFrameworkName
Gets or sets a string that specifies the target version and profile of the .NET Framework for the app...
string BaseDirectory
Gets the base directory that the assembly resolver uses to probe for assemblies.
Definition: AppDomain.cs:323
Definition: __Canon.cs:3
static bool TryGetSwitch(string switchName, out bool isEnabled)
Tries to get the value of a switch.
Definition: AppContext.cs:58
static AppDomain CurrentDomain
Gets the current application domain for the current T:System.Threading.Thread.
Definition: AppDomain.cs:274
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static string BaseDirectory
Gets the pathname of the base directory that the assembly resolver uses to probe for assemblies.
Definition: AppContext.cs:23
AppDomainSetup SetupInformation
Gets the application domain configuration information for this instance.
Definition: AppDomain.cs:431
static void SetSwitch(string switchName, bool isEnabled)
Sets the value of a switch.
Definition: AppContext.cs:112
The exception that is thrown when one of the arguments provided to a method is not valid.
static object GetData(string name)
Returns the value of the named data element assigned to the current application domain.
Definition: AppContext.cs:32
object GetData(string name)
Gets the value stored in the current application domain for the specified name.
Definition: AppDomain.cs:2542
static string TargetFrameworkName
Gets the name of the framework version targeted by the current application.
Definition: AppContext.cs:27