mscorlib(4.0.0.0) API with additions
LocalAppContext.cs
2 using System.Reflection;
4 
5 namespace System
6 {
7  internal static class LocalAppContext
8  {
9  private delegate bool TryGetSwitchDelegate(string switchName, out bool value);
10 
11  private static TryGetSwitchDelegate TryGetSwitchFromCentralAppContext;
12 
13  private static bool s_canForwardCalls;
14 
15  private static Dictionary<string, bool> s_switchMap;
16 
17  private static readonly object s_syncLock;
18 
19  private static bool DisableCaching
20  {
21  get;
22  set;
23  }
24 
25  static LocalAppContext()
26  {
27  s_switchMap = new Dictionary<string, bool>();
28  s_syncLock = new object();
29  s_canForwardCalls = SetupDelegate();
30  System.AppContextDefaultValues.PopulateDefaultValues();
31  DisableCaching = IsSwitchEnabled("TestSwitch.LocalAppContext.DisableCaching");
32  }
33 
34  public static bool IsSwitchEnabled(string switchName)
35  {
36  if (s_canForwardCalls && TryGetSwitchFromCentralAppContext(switchName, out bool value))
37  {
38  return value;
39  }
40  return IsSwitchEnabledLocal(switchName);
41  }
42 
43  private static bool IsSwitchEnabledLocal(string switchName)
44  {
45  bool flag;
46  bool value;
47  lock (s_switchMap)
48  {
49  flag = s_switchMap.TryGetValue(switchName, out value);
50  }
51  if (flag)
52  {
53  return value;
54  }
55  return false;
56  }
57 
58  private static bool SetupDelegate()
59  {
60  Type type = typeof(object).Assembly.GetType("System.AppContext");
61  if (type == null)
62  {
63  return false;
64  }
65  MethodInfo method = type.GetMethod("TryGetSwitch", BindingFlags.Static | BindingFlags.Public, null, new Type[2]
66  {
67  typeof(string),
68  typeof(bool).MakeByRefType()
69  }, null);
70  if (method == null)
71  {
72  return false;
73  }
74  TryGetSwitchFromCentralAppContext = (TryGetSwitchDelegate)Delegate.CreateDelegate(typeof(TryGetSwitchDelegate), method);
75  return true;
76  }
77 
78  [MethodImpl(MethodImplOptions.AggressiveInlining)]
79  internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
80  {
81  if (switchValue < 0)
82  {
83  return false;
84  }
85  if (switchValue > 0)
86  {
87  return true;
88  }
89  return GetCachedSwitchValueInternal(switchName, ref switchValue);
90  }
91 
92  private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
93  {
94  if (DisableCaching)
95  {
96  return IsSwitchEnabled(switchName);
97  }
98  bool flag = IsSwitchEnabled(switchName);
99  switchValue = (flag ? 1 : (-1));
100  return flag;
101  }
102 
103  internal static void DefineSwitchDefault(string switchName, bool initialValue)
104  {
105  s_switchMap[switchName] = initialValue;
106  }
107  }
108 }
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
MethodImplOptions
Defines the details of how a method is implemented.
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
Attribute can be applied to a delegate.