From a08882f2758ba9799e360e702d5f893d3910e2d8 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Tue, 8 Oct 2024 08:19:49 +0200 Subject: [PATCH] Remove obsolete TARGETTING35SP1 preprocessor directive and dead code contained within (#9744) --- .../RuntimeIdentifierPropertyAttribute.cs | 2 +- .../System/Xaml/AttachablePropertyServices.cs | 316 +----------------- .../Xaml/Context/ObjectWriterContext.cs | 20 -- .../Xaml/InfosetObjects/XamlObjectWriter.cs | 17 +- .../XamlObjectWriterSettings.cs | 25 +- .../System.Xaml/System/Xaml/XamlException.cs | 6 - .../System.Xaml/System/Xaml/XamlLanguage.cs | 38 --- .../System/Xaml/XamlSchemaContext.cs | 8 - 8 files changed, 19 insertions(+), 413 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RuntimeIdentifierPropertyAttribute.cs b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RuntimeIdentifierPropertyAttribute.cs index 0efbde45b03..63be6a4d974 100644 --- a/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RuntimeIdentifierPropertyAttribute.cs +++ b/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/RuntimeIdentifierPropertyAttribute.cs @@ -24,7 +24,7 @@ namespace System.Windows.Markup namespace System.Windows.Markup #endif { -#if !PBTCOMPILER && !TARGETTING35SP1 && !WINDOWS_BASE +#if !PBTCOMPILER && !WINDOWS_BASE /// /// This attribute is placed on a class to identify the property that will /// function as an Name for the given class diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/AttachablePropertyServices.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/AttachablePropertyServices.cs index 31bc91ee444..efdeaf838c4 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/AttachablePropertyServices.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/AttachablePropertyServices.cs @@ -118,10 +118,9 @@ public static bool TryGetProperty(object instance, AttachableMemberIdentifier } // DefaultAttachedPropertyStore is used by the global AttachedPropertyServices to implement - // global attached properties for types which don't implement IAttachedProperties or DO/Dependency Property - // integration for their attached properties. - // - #if !TARGETTING35SP1 + // global attached properties for types which don't implement IAttachedProperties or DO/Dependency Property + // integration for their attached properties. + sealed class DefaultAttachedPropertyStore { Lazy>> instanceStorage = @@ -237,314 +236,5 @@ public bool TryGetProperty(object instance, AttachableMemberIdentifier name, return false; } } -#else - //***********************************************WARNING************************************************************ - // In CLR4.0 there is ConditionalWeakTable. This implementation is for 3.5 and uses a WeakKey dictionary. - // This implementation does not handle the problem where a key is rooted in a value (or graph of a value). - // This will "leak" (never get cleaned up). If we ship a 3.5 version of System.Xaml.dll we should - // consider adding logic that detects such cycles on Add and throws. - //****************************************************************************************************************** - sealed class DefaultAttachedPropertyStore - { - Lazy>> instanceStorage = - new Lazy>>( - () => new WeakDictionary>(), LazyInitMode.AllowMultipleThreadSafeExecution); - - public void CopyPropertiesTo(object instance, KeyValuePair[] array, int index) - { - if (instanceStorage.IsInitialized) - { - lock (instanceStorage.Value.SyncObject) - { - Dictionary instanceProperties; - if (instanceStorage.Value.TryGetValue(instance, out instanceProperties)) - { - ((ICollection>)instanceProperties).CopyTo(array, index); - } - } - } - } - - public int GetPropertyCount(object instance) - { - if (instanceStorage.IsInitialized) - { - lock(instanceStorage.Value.SyncObject) - { - Dictionary instanceProperties; - if (instanceStorage.Value.TryGetValue(instance, out instanceProperties)) - { - return instanceProperties.Count; - } - } - } - return 0; - } - - // - // Remove the property 'name'. If the property doesn't exist it returns false. - // - public bool RemoveProperty(object instance, AttachableMemberIdentifier name) - { - if (instanceStorage.IsInitialized) - { - lock (instanceStorage.Value.SyncObject) - { - Dictionary instanceProperties; - if (instanceStorage.Value.TryGetValue(instance, out instanceProperties)) - { - return instanceProperties.Remove(name); - } - } - } - return false; - } - - // - // Set the property 'name' value to 'value', if the property doesn't currently exist this will add the property - // - public void SetProperty(object instance, AttachableMemberIdentifier name, object value) - { - // - // Accessing Value forces initialization - lock (instanceStorage.Value.SyncObject) - { - Dictionary instanceProperties; - if (!instanceStorage.Value.TryGetValue(instance, out instanceProperties)) - { - instanceProperties = new Dictionary(); - instanceStorage.Value.Add(instance, instanceProperties); - } - instanceProperties[name] = value; - } - } - - // - // Retrieve the value of the attached property 'name'. If there is not attached property then return false. - // - public bool TryGetProperty(object instance, AttachableMemberIdentifier name, out T value) - { - if (instanceStorage.IsInitialized) - { - lock (instanceStorage.Value.SyncObject) - { - Dictionary attachedProperties; - if (instanceStorage.Value.TryGetValue(instance, out attachedProperties)) - { - object valueAsObj; - if (attachedProperties.TryGetValue(name, out valueAsObj) && - valueAsObj is T) - { - value = (T)valueAsObj; - return true; - } - } - } - } - value = default(T); - return false; - } - - public class WeakDictionary - where K : class - where V : class - { - // Optimally this would be a _real_ dictionary implementation that when it ran across WeakKey's that had - // gone out of scope would immediately at least clear their value and from time to time schedule a real - // cleanup. - Dictionary storage; - object cleanupSyncObject; - - public WeakDictionary() - { - if (typeof(K) == typeof(WeakReference)) - { - throw new InvalidOperationException(); - } - - this.cleanupSyncObject = new object(); - storage = new Dictionary.WeakKey, V>(); - // kick off the cleanup process... - WeakDictionaryCleanupToken.CreateCleanupToken(this); - } - - public int Count - { - get { return storage.Count; } - } - - public void Add(K key, V value) - { - storage.Add(new WeakKey(key, false), value); - } - - internal object SyncObject - { - get { return this.cleanupSyncObject; } - } - - public void Cleanup() - { - List toClean = null; - - // First determine what items have died and can be removed... - lock(cleanupSyncObject) - { - foreach (var key in storage.Keys) - { - bool isAlive, needsCleanup; - key.GetValue(out isAlive, out needsCleanup); - if (!isAlive) - { - if (toClean == null) - { - toClean = new List(); - } - toClean.Add(key); - } - } - } - - if (toClean != null) - { - // Second go and remove them... - lock(cleanupSyncObject) - { - // If toClean is > some % of the total size it is probably better - // just to create a new dictionary and migrate some set of items over - // wholesale? It is unclear whether or not this is true. - foreach (var key in toClean) - { - storage.Remove(key); - } - } - } - - WeakDictionaryCleanupToken.CreateCleanupToken(this); - } - - public bool Remove(K key) - { - return storage.Remove(new WeakKey(key, true)); - } - - public bool TryGetValue(K key, out V value) - { - return storage.TryGetValue(new WeakKey(key, true), out value); - } - - public struct WeakKey : IEquatable - { - int hashCode; - object reference; - - public WeakKey(K key, bool lookup) - { - hashCode = key.GetHashCode(); - reference = lookup ? key : (object)new WeakReference(key); - } - - public override int GetHashCode() - { - return hashCode; - } - - public K GetValue(out bool isAlive, out bool needsCleanup) - { - if (reference == null) - { - isAlive = false; - needsCleanup = false; - return (K)reference; - } - - K value; - WeakReference wr = reference as WeakReference; - if (wr != null) - { - value = (K)wr.Target; - isAlive = value != null; - needsCleanup = !isAlive; - if (needsCleanup) - { - // This cleans up the WeakReference now that we know the - // target object is dead... - reference = null; - } - return value; - } - - value = reference as K; - if (value != null) - { - isAlive = true; - needsCleanup = false; - return value; - } - - throw new InvalidOperationException(); - } - - public bool Equals(WeakKey other) - { - WeakKey x = this; - WeakKey y = other; - bool xIsAlive, yIsAlive; - bool xNeedsCleanup, yNeedsCleanup; - K xKey = x.GetValue(out xIsAlive, out xNeedsCleanup); - K yKey = y.GetValue(out yIsAlive, out yNeedsCleanup); - - // If they are both not alive then they are equivalent - if (!xIsAlive && !yIsAlive) - { - return true; - } - - if (!xIsAlive || !yIsAlive) - { - return false; - } - - return xKey == yKey; - } - } - - // This cleanup token will be immediately thrown away and as a result it will - // (a couple of GCs later) make it into the finalization queue and when finalized - // will kick off a thread-pool job to cleanup the dictionary. - - public class WeakDictionaryCleanupToken - { - WeakDictionary storage; - - WeakDictionaryCleanupToken(WeakDictionary storage) - { - this.storage = storage; - } - - ~WeakDictionaryCleanupToken() - { - // Schedule cleanup - ThreadPool.QueueUserWorkItem((WaitCallback)delegate - { - storage.Cleanup(); - }); - } - - [SuppressMessage("Microsoft.Usage", "CA1806", - Justification = "The point of this method is to create one of these and let it float away... To be finalized...")] - [SuppressMessage("Microsoft.Performance", "CA1804", - Justification = "The point of this method is to create one of these and let it float away... To be finalized...")] - public static void CreateCleanupToken(WeakDictionary storage) - { - // Create one of these, the work is done when it is finalized which - // will be at some indeterminate point in the future... - WeakDictionaryCleanupToken token = new WeakDictionaryCleanupToken(storage); - } - } - - } - } -#endif } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs index e4bcae3d14c..efb5a16d29b 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs @@ -48,11 +48,7 @@ public ObjectWriterContext(XamlSavedContext savedContext, XAML3.INameScopeDictionary rootNameScopeDictionary = null; if (rootNameScope == null) { -#if TARGETTING35SP1 - rootNameScopeDictionary = new NameScopeDictionary(new NameScope()); -#else rootNameScopeDictionary = new NameScope(); -#endif } else { @@ -90,11 +86,7 @@ public ObjectWriterContext(XamlSchemaContext schemaContext, XAML3.INameScopeDictionary rootNameScopeDictionary = null; if (rootNameScope == null) { -#if TARGETTING35SP1 - rootNameScopeDictionary = new NameScopeDictionary(new NameScope()); -#else rootNameScopeDictionary = new NameScope(); -#endif } else { @@ -814,11 +806,7 @@ private XAML3.INameScopeDictionary LookupNameScopeDictionary(ObjectWriterFrame f if (frame.Depth == SavedDepth + 1 && _settings != null && !_settings.RegisterNamesOnExternalNamescope) { -#if TARGETTING35SP1 - frame.NameScopeDictionary = new NameScopeDictionary(new NameScope()); -#else frame.NameScopeDictionary = new NameScope(); -#endif } else { @@ -912,11 +900,7 @@ private XAML3.INameScopeDictionary HuntAroundForARootNameScope(ObjectWriterFrame XAML3.INameScope nameScope = (XAML3.INameScope)_runtime.GetValue(inst, nameScopeProperty, false); if (nameScope == null) { -#if TARGETTING35SP1 - nameScopeDictionary = new NameScopeDictionary(new NameScope()); -#else nameScopeDictionary = new NameScope(); -#endif _runtime.SetValue(inst, nameScopeProperty, nameScopeDictionary); } else @@ -942,11 +926,7 @@ private XAML3.INameScopeDictionary HuntAroundForARootNameScope(ObjectWriterFrame // for our own usage. For IXamlNameResolver() to use. if (nameScopeDictionary == null) { -#if TARGETTING35SP1 - nameScopeDictionary = new NameScopeDictionary(new NameScope()); -#else nameScopeDictionary = new NameScope(); -#endif } rootFrame.NameScopeDictionary = nameScopeDictionary; diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs index 35c6056a42a..1128436f8c7 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs @@ -30,9 +30,8 @@ public class XamlObjectWriter : XamlWriter, IXamlLineInfoConsumer, IAddLineInfo, private EventHandler _beforePropertiesHandler; private EventHandler _afterPropertiesHandler; private EventHandler _afterEndInitHandler; -#if !TARGETTING35SP1 private EventHandler _xamlSetValueHandler; -#endif + private object _rootObjectInstance; private bool _skipDuplicatePropertyCheck; NameFixupGraph _nameFixupGraph; @@ -86,9 +85,9 @@ void Initialize(XamlSchemaContext schemaContext, XamlSavedContext savedContext, _beforePropertiesHandler = settings.BeforePropertiesHandler; _afterPropertiesHandler = settings.AfterPropertiesHandler; _afterEndInitHandler = settings.AfterEndInitHandler; -#if !TARGETTING35SP1 + _xamlSetValueHandler = settings.XamlSetValueHandler; -#endif + _rootObjectInstance = settings.RootObjectInstance; _skipDuplicatePropertyCheck = settings.SkipDuplicatePropertyCheck; _skipProvideValueOnRoot = settings.SkipProvideValueOnRoot; @@ -125,7 +124,6 @@ private XamlRuntime CreateRuntime(XamlObjectWriterSettings settings, XamlSchemaC if (settings != null) { runtimeSettings = new XamlRuntimeSettings { IgnoreCanConvert = settings.IgnoreCanConvert }; -#if !TARGETTING35SP1 if (settings.AccessLevel != null) { result = new PartialTrustTolerantRuntime(runtimeSettings, settings.AccessLevel, schemaContext); @@ -133,7 +131,6 @@ private XamlRuntime CreateRuntime(XamlObjectWriterSettings settings, XamlSchemaC } if (result == null) { -#endif result = new ClrObjectRuntime(runtimeSettings, true /*isWriter*/); } result.LineInfo = this; @@ -178,7 +175,6 @@ protected virtual void OnAfterEndInit(object value) } } -#if !TARGETTING35SP1 protected virtual bool OnSetValue(object eventSender, XamlMember member, object value) { if (_xamlSetValueHandler != null) @@ -189,7 +185,6 @@ protected virtual bool OnSetValue(object eventSender, XamlMember member, object } return false; } -#endif private NameFixupGraph NameFixupGraph { @@ -1324,7 +1319,6 @@ private bool Logic_CreatePropertyValueFromValue(ObjectWriterContext ctx) XamlValueConverter converter = property.TypeConverter; object inst = null; -#if !TARGETTING35SP1 XamlType declaringType = null; if (property.IsAttachable) { @@ -1367,7 +1361,6 @@ private bool Logic_CreatePropertyValueFromValue(ObjectWriterContext ctx) } } } -#endif if (converter != null) { @@ -1659,7 +1652,6 @@ private bool Logic_ProvideValue(ObjectWriterContext ctx) object parentInstance = ctx.ParentInstance; XamlMember parentProperty = ctx.ParentProperty; -#if !TARGETTING35SP1 if (parentProperty != null && parentProperty.IsUnknown == false) { XamlType declaringType = null; @@ -1687,7 +1679,6 @@ private bool Logic_ProvideValue(ObjectWriterContext ctx) } } } -#endif // The Service Provider Interface IProvideValueTarget requires that we can supply ProvideValue with the // instance of the left-hand side of the property assignment. Markup extensions that are assigned to // directives are allowed to have a null left-hand instance. @@ -2161,7 +2152,6 @@ private void Logic_SetConnectionId(ObjectWriterContext ctx, int connectionId, ob private void SetValue(object inst, XamlMember property, object value) { -#if !TARGETTING35SP1 if (!property.IsDirective) { if (OnSetValue(inst, property, value)) @@ -2169,7 +2159,6 @@ private void SetValue(object inst, XamlMember property, object value) return; } } -#endif Runtime.SetValue(inst, property, value); } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriterSettings.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriterSettings.cs index c5f3c891c90..258afdf9dad 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriterSettings.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriterSettings.cs @@ -22,9 +22,9 @@ public XamlObjectWriterSettings(XamlObjectWriterSettings settings) BeforePropertiesHandler = settings.BeforePropertiesHandler; AfterPropertiesHandler = settings.AfterPropertiesHandler; AfterEndInitHandler = settings.AfterEndInitHandler; -#if !TARGETTING35SP1 + XamlSetValueHandler = settings.XamlSetValueHandler; -#endif + RootObjectInstance = settings.RootObjectInstance; IgnoreCanConvert = settings.IgnoreCanConvert; ExternalNameScope = settings.ExternalNameScope; @@ -40,9 +40,7 @@ public XamlObjectWriterSettings(XamlObjectWriterSettings settings) public EventHandler BeforePropertiesHandler { get; set; } public EventHandler AfterPropertiesHandler { get; set; } public EventHandler AfterEndInitHandler { get; set; } -#if !TARGETTING35SP1 public EventHandler XamlSetValueHandler { get; set; } -#endif public Object RootObjectInstance { get; set; } public bool IgnoreCanConvert { get; set; } @@ -65,15 +63,16 @@ public XamlObjectWriterSettings(XamlObjectWriterSettings settings) internal XamlObjectWriterSettings StripDelegates() { - XamlObjectWriterSettings result = new XamlObjectWriterSettings(this); - // We need better protection against leaking out these delegates - result.AfterBeginInitHandler = null; - result.AfterEndInitHandler = null; - result.AfterPropertiesHandler = null; - result.BeforePropertiesHandler = null; -#if !TARGETTING35SP1 - result.XamlSetValueHandler = null; -#endif + XamlObjectWriterSettings result = new XamlObjectWriterSettings(this) + { + // We need better protection against leaking out these delegates + AfterBeginInitHandler = null, + AfterEndInitHandler = null, + AfterPropertiesHandler = null, + BeforePropertiesHandler = null, + XamlSetValueHandler = null + }; + return result; } } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlException.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlException.cs index 9ea84ff7a2d..40f4fee708f 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlException.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlException.cs @@ -73,9 +73,6 @@ protected XamlException(SerializationInfo info, StreamingContext context) } #pragma warning restore SYSLIB0051 // Type or member is obsolete -#if TARGETTING35SP1 -#else -#endif #pragma warning disable CS0672 // Member overrides obsolete member #pragma warning disable SYSLIB0051 // Type or member is obsolete public override void GetObjectData(SerializationInfo info, StreamingContext context) @@ -167,9 +164,6 @@ protected XamlDuplicateMemberException(SerializationInfo info, StreamingContext ParentType = (XamlType)info.GetValue("ParentType", typeof(XamlType)); } -#if TARGETTING35SP1 -#else -#endif #pragma warning disable CS0672 // Member overrides obsolete member #pragma warning disable SYSLIB0051 // Type or member is obsolete public override void GetObjectData(SerializationInfo info, StreamingContext context) diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlLanguage.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlLanguage.cs index bffce8ad4d8..51301bba292 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlLanguage.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlLanguage.cs @@ -462,42 +462,4 @@ private static XamlType GetXamlType(Type type) return result; } } - - -#if TARGETTING35SP1 - public delegate T Initializer(); - - public struct Lazy where T : class - { - private Initializer m_init; - private T m_value; - public bool IsValueCreated - { - get { return m_value != null; } - } - public Lazy(Initializer init) - { - m_init = init; - m_value = null; - } - - public T Value - { - get - { - if (m_value == null) - { - T newValue = m_init(); - if (Interlocked.CompareExchange(ref m_value, newValue, null) != null && - newValue is IDisposable) - { - ((IDisposable)newValue).Dispose(); - } - } - return m_value; - } - } - } -#endif - } diff --git a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs index cb0d26973e4..9f27c75a174 100644 --- a/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs +++ b/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs @@ -14,9 +14,7 @@ using System.Xaml.MS.Impl; using System.Xaml.Schema; using MS.Internal.Xaml.Parser; -#if !TARGETTING35SP1 using System.Collections.Concurrent; -#endif namespace System.Xaml { @@ -1361,17 +1359,11 @@ private void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args) } } -#if TARGETTING35SP1 -#else -#endif public void Hook() { AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad; } -#if TARGETTING35SP1 -#else -#endif public void Unhook() { AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;