diff --git a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs index c01e87488ec..fe708394d74 100644 --- a/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs +++ b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/UncommonField.cs @@ -2,10 +2,9 @@ // 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.Collections.Generic; -using System.Diagnostics; - +using System.Runtime.CompilerServices; +using MS.Internal.KnownBoxes; using MS.Internal.WindowsBase; // for FriendAccessAllowed namespace System.Windows @@ -54,12 +53,24 @@ public void SetValue(DependencyObject instance, T value) // Set the value if it's not the default, otherwise remove the value. if (!EqualityComparer.Default.Equals(value, _defaultValue)) { - instance.SetEffectiveValue(entryIndex, null /* dp */, _globalIndex, null /* metadata */, value, BaseValueSourceInternal.Local); + object valueObject; + + if (typeof(T) == typeof(bool)) + { + // Use shared boxed instances rather than creating new objects for each SetValue call. + valueObject = BooleanBoxes.Box(Unsafe.As(ref value)); + } + else + { + valueObject = value; + } + + instance.SetEffectiveValue(entryIndex, dp: null, _globalIndex, metadata: null, valueObject, BaseValueSourceInternal.Local); _hasBeenSet = true; } else { - instance.UnsetEffectiveValue(entryIndex, null /* dp */, null /* metadata */); + instance.UnsetEffectiveValue(entryIndex, dp: null, metadata: null); } } else