Skip to content

Commit 24105c1

Browse files
authored
Remove AppContextDefaultValues/AccessibilitySwitches values from NetFX (dead code) (#9866)
* Remove AppContextDefaultValues/AccessibilitySwitches values from NetFX (dead code) * Constrain CS0436 to LocalAppContext; use proper locking scheme mechanism * Clean up CS0436 warning suppressions from the separate context files * Change LocalAppContext from instance/partial to static class
1 parent 4841c5d commit 24105c1

File tree

11 files changed

+74
-273
lines changed

11 files changed

+74
-273
lines changed

src/Microsoft.DotNet.Wpf/src/Common/src/System/AppContextDefaultValues.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
// There are cases where we have multiple assemblies that are going to import this file and
6-
// if they are going to also have InternalsVisibleTo between them, there will be a compiler warning
7-
// that the type is found both in the source and in a referenced assembly. The compiler will prefer
8-
// the version of the type defined in the source
9-
//
10-
// In order to disable the warning for this type we are disabling this warning for this entire file.
11-
#pragma warning disable 436
12-
13-
using System;
14-
using System.Collections.Generic;
15-
165
namespace System
176
{
187
internal static partial class AppContextDefaultValues
@@ -182,15 +171,11 @@ private static bool TryParseFrameworkName(String frameworkName, out String ident
182171
return true;
183172
}
184173
#endif
185-
// This is a partial method. Platforms (such as Desktop) can provide an implementation of it that will read override value
186-
// from whatever mechanism is available on that platform. If no implementation is provided, the compiler is going to remove the calls
187-
// to it from the code
188-
static partial void TryGetSwitchOverridePartial(string switchName, ref bool overrideFound, ref bool overrideValue);
189174

175+
/// <summary>
190176
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
191177
/// It is partial because each library should define this method in their code to contain their defaults.
178+
/// </summary>
192179
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion);
193180
}
194181
}
195-
196-
#pragma warning restore 436

src/Microsoft.DotNet.Wpf/src/Common/src/System/LocalAppContext.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,58 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Runtime.CompilerServices;
6-
using System.Threading;
76
using System.Collections.Generic;
7+
using System.Threading;
88

99
namespace System
1010
{
11-
// error CS0436: When building PresentationFramework, the type 'LocalAppContext'
12-
// conflicts with the imported type 'LocalAppContext' in 'PresentationCore
13-
#pragma warning disable 436
14-
internal partial class LocalAppContext
11+
internal static class LocalAppContext
1512
{
16-
private static Dictionary<string, bool> s_switchMap = new Dictionary<string, bool>();
17-
private static readonly object s_syncLock = new object();
13+
/// <summary>
14+
/// Holds the switch names and their values. In case it is modified outside <see cref="DefineSwitchDefault"/>,
15+
/// proper thread synchronization is required as the switch state can be queried from any thread.
16+
/// </summary>
17+
private static readonly Dictionary<string, bool> s_switchMap = new();
18+
#if !NETFX
19+
private static readonly Lock s_syncLock = new();
20+
#else
21+
private static readonly object s_syncLock = new();
22+
#endif
1823

1924
private static bool DisableCaching { get; set; }
2025

2126
static LocalAppContext()
2227
{
28+
// When building PresentationFramework, 'LocalAppContext' from WindowsBase.dll conflicts
29+
// with 'LocalAppContext' from PresentationCore.dll since there is InternalsVisibleTo set
30+
#pragma warning disable CS0436 // Type conflicts with imported type
31+
2332
// Populate the default values of the local app context
2433
AppContextDefaultValues.PopulateDefaultValues();
2534

35+
#pragma warning restore CS0436 // Type conflicts with imported type
36+
2637
// Cache the value of the switch that help with testing
2738
DisableCaching = IsSwitchEnabled(@"TestSwitch.LocalAppContext.DisableCaching");
2839
}
2940

3041
public static bool IsSwitchEnabled(string switchName)
3142
{
32-
if (System.AppContext.TryGetSwitch(switchName, out var isEnabledCentrally))
43+
if (AppContext.TryGetSwitch(switchName, out bool isEnabledCentrally))
3344
{
3445
// we found the switch, so return whatever value it has
3546
return isEnabledCentrally;
3647
}
37-
// if we could not get the value from the central authority, try the local storage.
3848

49+
// if we could not get the value from the central authority, try the local storage.
3950
return IsSwitchEnabledLocal(switchName);
4051
}
4152

4253
private static bool IsSwitchEnabledLocal(string switchName)
4354
{
4455
// read the value from the set of local defaults
4556
bool isEnabled, isPresent;
46-
lock (s_switchMap)
57+
lock (s_syncLock)
4758
{
4859
isPresent = s_switchMap.TryGetValue(switchName, out isEnabled);
4960
}
@@ -62,20 +73,22 @@ private static bool IsSwitchEnabledLocal(string switchName)
6273
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6374
internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
6475
{
65-
if (switchValue < 0) return false;
66-
if (switchValue > 0) return true;
76+
if (switchValue < 0)
77+
return false;
78+
if (switchValue > 0)
79+
return true;
6780

6881
return GetCachedSwitchValueInternal(switchName, ref switchValue);
6982
}
7083

7184
private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
7285
{
73-
if (LocalAppContext.DisableCaching)
86+
if (DisableCaching)
7487
{
75-
return LocalAppContext.IsSwitchEnabled(switchName);
88+
return IsSwitchEnabled(switchName);
7689
}
7790

78-
bool isEnabled = LocalAppContext.IsSwitchEnabled(switchName);
91+
bool isEnabled = IsSwitchEnabled(switchName);
7992
switchValue = isEnabled ? 1 /*true*/ : -1 /*false*/;
8093
return isEnabled;
8194
}
@@ -90,5 +103,4 @@ internal static void DefineSwitchDefault(string switchName, bool initialValue)
90103
s_switchMap[switchName] = initialValue;
91104
}
92105
}
93-
#pragma warning restore 436
94106
}

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/BuildTasksAppContextSwitches.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
namespace MS.Internal
99
{
10-
// WPF's builds are seeing warnings as a result of using LocalAppContext in mutliple assemblies.
11-
// that have internalsVisibleTo attribute set between them - which results in the warning.
12-
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
13-
// AppContextDefaultValues have pragmas added to suppress warning 436
14-
#pragma warning disable 436
1510
internal static class BuildTasksAppContextSwitches
1611
{
1712
#region DoNotUseSha256ForMarkupCompilerChecksumAlgorithm
@@ -30,5 +25,4 @@ public static bool DoNotUseSha256ForMarkupCompilerChecksumAlgorithm
3025

3126
#endregion
3227
}
33-
#pragma warning restore 436
3428
}

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/System/AppContextDefaultValues.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,31 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
6-
using System.Windows;
75
using MS.Internal;
86

97
namespace System
108
{
11-
// WPF's builds are seeing warnings as a result of using LocalAppContext in mutliple assemblies.
12-
// that have internalsVisibleTo attribute set between them - which results in the warning.
13-
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
14-
// AppContextDefaultValues have pragmas added to suppress warning 436
15-
#pragma warning disable 436
169
internal static partial class AppContextDefaultValues
1710
{
11+
/// <summary>
12+
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
13+
/// It is partial because each library should define this method in their code to contain their defaults.
14+
/// </summary>
1815
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion)
1916
{
2017
switch (platformIdentifier)
2118
{
2219
case ".NETFramework":
20+
if (targetFrameworkVersion <= 40701)
2321
{
24-
if (targetFrameworkVersion <= 40701)
25-
{
26-
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, true);
27-
}
28-
29-
break;
22+
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, true);
3023
}
24+
break;
3125

3226
case ".NETCoreApp":
33-
{
34-
InitializeNetFxSwitchDefaultsForNetCoreRuntime();
35-
}
27+
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, false);
3628
break;
3729
}
3830
}
39-
40-
private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
41-
{
42-
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, false);
43-
}
4431
}
45-
#pragma warning restore 436
4632
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/CoreAppContextSwitches.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
//
6-
//
7-
85
using System;
96
using System.Runtime.CompilerServices;
107
using System.Windows;
118

129
namespace MS.Internal
1310
{
14-
// WPF's builds are seeing new warnings as as result of using LocalAppContext in PresentationFramework, PresentationCore and WindowsBase.
15-
// These binaries have internalsVisibleTo attribute set between them - which results in the warning.
16-
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
17-
// AppContextDefaultValues have pragmas added to suppress warning 436
18-
#pragma warning disable 436
1911
internal static class CoreAppContextSwitches
2012
{
2113
#region DoNotScaleForDpiChanges
@@ -411,5 +403,4 @@ public static bool DisableSpecialCharacterLigature
411403
#endregion
412404

413405
}
414-
#pragma warning restore 436
415406
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/AppContextDefaultValues.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Windows;
65
using MS.Internal;
76

87
namespace System
98
{
10-
// WPF's builds are seeing new warnings as as result of using LocalAppContext in PresentationFramework, PresentationCore and WindowsBase.
11-
// These binaries have internalsVisibleTo attribute set between them - which results in the warning.
12-
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
13-
// AppContextDefaultValues have pragmas added to suppress warning 436
14-
#pragma warning disable 436
159
internal static partial class AppContextDefaultValues
1610
{
11+
/// <summary>
12+
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
13+
/// It is partial because each library should define this method in their code to contain their defaults.
14+
/// </summary>
1715
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion)
18-
{
19-
switch (platformIdentifier)
20-
{
21-
case ".NETFramework":
22-
{
23-
if (targetFrameworkVersion <= 40601)
24-
{
25-
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotScaleForDpiChangesSwitchName, true);
26-
}
27-
28-
if (targetFrameworkVersion <= 40602)
29-
{
30-
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.OverrideExceptionWithNullReferenceExceptionName, true);
31-
}
32-
33-
if (targetFrameworkVersion <= 40702)
34-
{
35-
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier2OrGreaterSwitchName, true);
36-
}
37-
38-
break;
39-
}
40-
case ".NETCoreApp":
41-
{
42-
InitializeNetFxSwitchDefaultsForNetCoreRuntime();
43-
}
44-
break;
45-
}
46-
}
47-
48-
private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
4916
{
5017
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotScaleForDpiChangesSwitchName, false);
5118
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.OverrideExceptionWithNullReferenceExceptionName, false);
@@ -64,5 +31,4 @@ private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
6431
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DisableSpecialCharacterLigatureSwitchName, false);
6532
}
6633
}
67-
#pragma warning restore 436
6834
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/FrameworkAppContextSwitches.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
6-
using MS.Internal.PresentationFramework.Interop;
7-
using System;
85
using System.Runtime.CompilerServices;
9-
using System.Windows;
6+
using System;
7+
8+
// When building PresentationFramework, 'LocalAppContext' from WindowsBase.dll conflicts
9+
// with 'LocalAppContext' from PresentationCore.dll since there is InternalsVisibleTo set
10+
#pragma warning disable CS0436 // Type conflicts with imported type
1011

1112
namespace MS.Internal
1213
{
13-
// There are cases where we have multiple assemblies that are going to import this file and
14-
// if they are going to also have InternalsVisibleTo between them, there will be a compiler warning
15-
// that the type is found both in the source and in a referenced assembly. The compiler will prefer
16-
// the version of the type defined in the source
17-
//
18-
// In order to disable the warning for this type we are disabling this warning for this entire file.
19-
#pragma warning disable 436
20-
2114
internal static class FrameworkAppContextSwitches
2215
{
2316
internal const string DoNotApplyLayoutRoundingToMarginsAndBorderThicknessSwitchName = "Switch.MS.Internal.DoNotApplyLayoutRoundingToMarginsAndBorderThickness";
@@ -156,6 +149,6 @@ public static bool DisableDynamicResourceOptimization
156149
}
157150
}
158151
}
159-
160-
#pragma warning restore 436
161152
}
153+
154+
#pragma warning restore CS0436 // Type conflicts with imported type

0 commit comments

Comments
 (0)