mscorlib(4.0.0.0) API with additions
AppContextDefaultValues.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
4 using System.Security;
5 using System.Text;
6 
7 namespace System
8 {
9  internal static class AppContextDefaultValues
10  {
11  internal static readonly string SwitchNoAsyncCurrentCulture = "Switch.System.Globalization.NoAsyncCurrentCulture";
12 
13  internal static readonly string SwitchThrowExceptionIfDisposedCancellationTokenSource = "Switch.System.Threading.ThrowExceptionIfDisposedCancellationTokenSource";
14 
15  internal static readonly string SwitchPreserveEventListnerObjectIdentity = "Switch.System.Diagnostics.EventSource.PreserveEventListnerObjectIdentity";
16 
17  internal static readonly string SwitchUseLegacyPathHandling = "Switch.System.IO.UseLegacyPathHandling";
18 
19  internal static readonly string SwitchBlockLongPaths = "Switch.System.IO.BlockLongPaths";
20 
21  internal static readonly string SwitchDoNotAddrOfCspParentWindowHandle = "Switch.System.Security.Cryptography.DoNotAddrOfCspParentWindowHandle";
22 
23  internal static readonly string SwitchSetActorAsReferenceWhenCopyingClaimsIdentity = "Switch.System.Security.ClaimsIdentity.SetActorAsReferenceWhenCopyingClaimsIdentity";
24 
25  private static volatile bool s_errorReadingRegistry;
26 
27  public static void PopulateDefaultValues()
28  {
29  ParseTargetFrameworkName(out string identifier, out string profile, out int version);
30  PopulateDefaultValuesPartial(identifier, profile, version);
31  }
32 
33  private static void ParseTargetFrameworkName(out string identifier, out string profile, out int version)
34  {
35  string targetFrameworkName = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
36  if (!TryParseFrameworkName(targetFrameworkName, out identifier, out version, out profile))
37  {
38  identifier = ".NETFramework";
39  version = 40000;
40  profile = string.Empty;
41  }
42  }
43 
44  private static bool TryParseFrameworkName(string frameworkName, out string identifier, out int version, out string profile)
45  {
46  identifier = (profile = string.Empty);
47  version = 0;
48  if (frameworkName == null || frameworkName.Length == 0)
49  {
50  return false;
51  }
52  string[] array = frameworkName.Split(',');
53  version = 0;
54  if (array.Length < 2 || array.Length > 3)
55  {
56  return false;
57  }
58  identifier = array[0].Trim();
59  if (identifier.Length == 0)
60  {
61  return false;
62  }
63  bool flag = false;
64  profile = null;
65  for (int i = 1; i < array.Length; i++)
66  {
67  string[] array2 = array[i].Split('=');
68  if (array2.Length != 2)
69  {
70  return false;
71  }
72  string text = array2[0].Trim();
73  string text2 = array2[1].Trim();
74  if (text.Equals("Version", StringComparison.OrdinalIgnoreCase))
75  {
76  flag = true;
77  if (text2.Length > 0 && (text2[0] == 'v' || text2[0] == 'V'))
78  {
79  text2 = text2.Substring(1);
80  }
81  Version version2 = new Version(text2);
82  version = version2.Major * 10000;
83  if (version2.Minor > 0)
84  {
85  version += version2.Minor * 100;
86  }
87  if (version2.Build > 0)
88  {
89  version += version2.Build;
90  }
91  }
92  else
93  {
94  if (!text.Equals("Profile", StringComparison.OrdinalIgnoreCase))
95  {
96  return false;
97  }
98  if (!string.IsNullOrEmpty(text2))
99  {
100  profile = text2;
101  }
102  }
103  }
104  if (!flag)
105  {
106  return false;
107  }
108  return true;
109  }
110 
111  [SecuritySafeCritical]
112  private static void TryGetSwitchOverridePartial(string switchName, ref bool overrideFound, ref bool overrideValue)
113  {
114  string text = null;
115  overrideFound = false;
116  if (!s_errorReadingRegistry)
117  {
118  text = GetSwitchValueFromRegistry(switchName);
119  }
120  if (text == null)
121  {
122  text = CompatibilitySwitch.GetValue(switchName);
123  }
124  if (text != null && bool.TryParse(text, out bool result))
125  {
126  overrideValue = result;
127  overrideFound = true;
128  }
129  }
130 
131  private static void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int version)
132  {
133  if (!(platformIdentifier == ".NETCore") && !(platformIdentifier == ".NETFramework"))
134  {
135  if ((platformIdentifier == "WindowsPhone" || platformIdentifier == "WindowsPhoneApp") && version <= 80100)
136  {
137  AppContext.DefineSwitchDefault(SwitchNoAsyncCurrentCulture, isEnabled: true);
138  AppContext.DefineSwitchDefault(SwitchThrowExceptionIfDisposedCancellationTokenSource, isEnabled: true);
139  AppContext.DefineSwitchDefault(SwitchUseLegacyPathHandling, isEnabled: true);
140  AppContext.DefineSwitchDefault(SwitchBlockLongPaths, isEnabled: true);
141  AppContext.DefineSwitchDefault(SwitchDoNotAddrOfCspParentWindowHandle, isEnabled: true);
142  }
143  }
144  else
145  {
146  if (version <= 40502)
147  {
148  AppContext.DefineSwitchDefault(SwitchNoAsyncCurrentCulture, isEnabled: true);
149  AppContext.DefineSwitchDefault(SwitchThrowExceptionIfDisposedCancellationTokenSource, isEnabled: true);
150  }
151  if (version <= 40601)
152  {
153  AppContext.DefineSwitchDefault(SwitchUseLegacyPathHandling, isEnabled: true);
154  AppContext.DefineSwitchDefault(SwitchBlockLongPaths, isEnabled: true);
155  AppContext.DefineSwitchDefault(SwitchSetActorAsReferenceWhenCopyingClaimsIdentity, isEnabled: true);
156  }
157  if (version <= 40602)
158  {
159  AppContext.DefineSwitchDefault(SwitchDoNotAddrOfCspParentWindowHandle, isEnabled: true);
160  }
161  }
162  PopulateOverrideValuesPartial();
163  }
164 
165  [SecuritySafeCritical]
166  private static void PopulateOverrideValuesPartial()
167  {
168  string appContextOverridesInternalCall = CompatibilitySwitch.GetAppContextOverridesInternalCall();
169  if (string.IsNullOrEmpty(appContextOverridesInternalCall))
170  {
171  return;
172  }
173  bool flag = false;
174  bool flag2 = false;
175  bool flag3 = false;
176  int num = -1;
177  int num2 = -1;
178  for (int i = 0; i <= appContextOverridesInternalCall.Length; i++)
179  {
180  if (i == appContextOverridesInternalCall.Length || appContextOverridesInternalCall[i] == ';')
181  {
182  if ((flag && flag2) & flag3)
183  {
184  int startIndex = num + 1;
185  int length = num2 - num - 1;
186  string switchName = appContextOverridesInternalCall.Substring(startIndex, length);
187  int startIndex2 = num2 + 1;
188  int length2 = i - num2 - 1;
189  string value = appContextOverridesInternalCall.Substring(startIndex2, length2);
190  if (bool.TryParse(value, out bool result))
191  {
192  AppContext.DefineSwitchOverride(switchName, result);
193  }
194  }
195  num = i;
196  flag2 = (flag3 = (flag = false));
197  }
198  else if (appContextOverridesInternalCall[i] == '=')
199  {
200  if (!flag)
201  {
202  flag = true;
203  num2 = i;
204  }
205  }
206  else if (flag)
207  {
208  flag3 = true;
209  }
210  else
211  {
212  flag2 = true;
213  }
214  }
215  }
216 
217  public static bool TryGetSwitchOverride(string switchName, out bool overrideValue)
218  {
219  overrideValue = false;
220  bool overrideFound = false;
221  TryGetSwitchOverridePartial(switchName, ref overrideFound, ref overrideValue);
222  return overrideFound;
223  }
224 
225  [SecuritySafeCritical]
226  private static string GetSwitchValueFromRegistry(string switchName)
227  {
228  try
229  {
230  using (SafeRegistryHandle hKey = new SafeRegistryHandle((IntPtr)(-2147483646), ownsHandle: true))
231  {
232  SafeRegistryHandle hkResult = null;
233  if (Win32Native.RegOpenKeyEx(hKey, "SOFTWARE\\Microsoft\\.NETFramework\\AppContext", 0, 131097, out hkResult) == 0)
234  {
235  int lpcbData = 12;
236  int lpType = 0;
237  StringBuilder stringBuilder = new StringBuilder(lpcbData);
238  if (Win32Native.RegQueryValueEx(hkResult, switchName, null, ref lpType, stringBuilder, ref lpcbData) == 0)
239  {
240  return stringBuilder.ToString();
241  }
242  }
243  else
244  {
245  s_errorReadingRegistry = true;
246  }
247  }
248  }
249  catch
250  {
251  s_errorReadingRegistry = true;
252  }
253  return null;
254  }
255  }
256 }
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Definition: __Canon.cs:3
Represents a mutable string of characters. This class cannot be inherited.To browse the ....