Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// There are cases where we have multiple assemblies that are going to import this file and
// if they are going to also have InternalsVisibleTo between them, there will be a compiler warning
// that the type is found both in the source and in a referenced assembly. The compiler will prefer
// the version of the type defined in the source
//
// In order to disable the warning for this type we are disabling this warning for this entire file.
#pragma warning disable 436

using System;
using System.Collections.Generic;

namespace System
{
internal static partial class AppContextDefaultValues
Expand Down Expand Up @@ -182,15 +171,11 @@ private static bool TryParseFrameworkName(String frameworkName, out String ident
return true;
}
#endif
// This is a partial method. Platforms (such as Desktop) can provide an implementation of it that will read override value
// from whatever mechanism is available on that platform. If no implementation is provided, the compiler is going to remove the calls
// to it from the code
static partial void TryGetSwitchOverridePartial(string switchName, ref bool overrideFound, ref bool overrideValue);

/// <summary>
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
/// It is partial because each library should define this method in their code to contain their defaults.
/// </summary>
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion);
}
}

#pragma warning restore 436
44 changes: 28 additions & 16 deletions src/Microsoft.DotNet.Wpf/src/Common/src/System/LocalAppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,58 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
using System.Threading;
using System.Collections.Generic;
using System.Threading;

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

private static bool DisableCaching { get; set; }

static LocalAppContext()
{
// When building PresentationFramework, 'LocalAppContext' from WindowsBase.dll conflicts
// with 'LocalAppContext' from PresentationCore.dll since there is InternalsVisibleTo set
#pragma warning disable CS0436 // Type conflicts with imported type

// Populate the default values of the local app context
AppContextDefaultValues.PopulateDefaultValues();

#pragma warning restore CS0436 // Type conflicts with imported type

// Cache the value of the switch that help with testing
DisableCaching = IsSwitchEnabled(@"TestSwitch.LocalAppContext.DisableCaching");
}

public static bool IsSwitchEnabled(string switchName)
{
if (System.AppContext.TryGetSwitch(switchName, out var isEnabledCentrally))
if (AppContext.TryGetSwitch(switchName, out bool isEnabledCentrally))
{
// we found the switch, so return whatever value it has
return isEnabledCentrally;
}
// if we could not get the value from the central authority, try the local storage.

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

private static bool IsSwitchEnabledLocal(string switchName)
{
// read the value from the set of local defaults
bool isEnabled, isPresent;
lock (s_switchMap)
lock (s_syncLock)
{
isPresent = s_switchMap.TryGetValue(switchName, out isEnabled);
}
Expand All @@ -62,20 +73,22 @@ private static bool IsSwitchEnabledLocal(string switchName)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
{
if (switchValue < 0) return false;
if (switchValue > 0) return true;
if (switchValue < 0)
return false;
if (switchValue > 0)
return true;

return GetCachedSwitchValueInternal(switchName, ref switchValue);
}

private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
{
if (LocalAppContext.DisableCaching)
if (DisableCaching)
{
return LocalAppContext.IsSwitchEnabled(switchName);
return IsSwitchEnabled(switchName);
}

bool isEnabled = LocalAppContext.IsSwitchEnabled(switchName);
bool isEnabled = IsSwitchEnabled(switchName);
switchValue = isEnabled ? 1 /*true*/ : -1 /*false*/;
return isEnabled;
}
Expand All @@ -90,5 +103,4 @@ internal static void DefineSwitchDefault(string switchName, bool initialValue)
s_switchMap[switchName] = initialValue;
}
}
#pragma warning restore 436
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

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

#endregion
}
#pragma warning restore 436
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,31 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.Windows;
using MS.Internal;

namespace System
{
// WPF's builds are seeing warnings as a result of using LocalAppContext in mutliple assemblies.
// that have internalsVisibleTo attribute set between them - which results in the warning.
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
// AppContextDefaultValues have pragmas added to suppress warning 436
#pragma warning disable 436
internal static partial class AppContextDefaultValues
{
/// <summary>
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
/// It is partial because each library should define this method in their code to contain their defaults.
/// </summary>
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion)
{
switch (platformIdentifier)
{
case ".NETFramework":
if (targetFrameworkVersion <= 40701)
{
if (targetFrameworkVersion <= 40701)
{
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, true);
}

break;
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, true);
}
break;

case ".NETCoreApp":
{
InitializeNetFxSwitchDefaultsForNetCoreRuntime();
}
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, false);
break;
}
}

private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
{
LocalAppContext.DefineSwitchDefault(BuildTasksAppContextSwitches.DoNotUseSha256ForMarkupCompilerChecksumAlgorithmSwitchName, false);
}
}
#pragma warning restore 436
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
//

using System;
using System.Runtime.CompilerServices;
using System.Windows;

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

}
#pragma warning restore 436
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Windows;
using MS.Internal;

namespace System
{
// WPF's builds are seeing new warnings as as result of using LocalAppContext in PresentationFramework, PresentationCore and WindowsBase.
// These binaries have internalsVisibleTo attribute set between them - which results in the warning.
// We don't have a way of suppressing this warning effectively until the shared copies of LocalAppContext and
// AppContextDefaultValues have pragmas added to suppress warning 436
#pragma warning disable 436
internal static partial class AppContextDefaultValues
{
/// <summary>
/// This is a partial method. This method is responsible for populating the default values based on a TFM.
/// It is partial because each library should define this method in their code to contain their defaults.
/// </summary>
static partial void PopulateDefaultValuesPartial(string platformIdentifier, string profile, int targetFrameworkVersion)
{
switch (platformIdentifier)
{
case ".NETFramework":
{
if (targetFrameworkVersion <= 40601)
{
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotScaleForDpiChangesSwitchName, true);
}

if (targetFrameworkVersion <= 40602)
{
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.OverrideExceptionWithNullReferenceExceptionName, true);
}

if (targetFrameworkVersion <= 40702)
{
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier2OrGreaterSwitchName, true);
}

break;
}
case ".NETCoreApp":
{
InitializeNetFxSwitchDefaultsForNetCoreRuntime();
}
break;
}
}

private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
{
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotScaleForDpiChangesSwitchName, false);
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.OverrideExceptionWithNullReferenceExceptionName, false);
Expand All @@ -64,5 +31,4 @@ private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DisableSpecialCharacterLigatureSwitchName, false);
}
}
#pragma warning restore 436
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using MS.Internal.PresentationFramework.Interop;
using System;
using System.Runtime.CompilerServices;
using System.Windows;
using System;

// When building PresentationFramework, 'LocalAppContext' from WindowsBase.dll conflicts
// with 'LocalAppContext' from PresentationCore.dll since there is InternalsVisibleTo set
#pragma warning disable CS0436 // Type conflicts with imported type

namespace MS.Internal
{
// There are cases where we have multiple assemblies that are going to import this file and
// if they are going to also have InternalsVisibleTo between them, there will be a compiler warning
// that the type is found both in the source and in a referenced assembly. The compiler will prefer
// the version of the type defined in the source
//
// In order to disable the warning for this type we are disabling this warning for this entire file.
#pragma warning disable 436

internal static class FrameworkAppContextSwitches
{
internal const string DoNotApplyLayoutRoundingToMarginsAndBorderThicknessSwitchName = "Switch.MS.Internal.DoNotApplyLayoutRoundingToMarginsAndBorderThickness";
Expand Down Expand Up @@ -156,6 +149,6 @@ public static bool DisableDynamicResourceOptimization
}
}
}

#pragma warning restore 436
}

#pragma warning restore CS0436 // Type conflicts with imported type
Loading