Skip to content

Commit

Permalink
Use generic collections in System.ComponentModel.TypeConverter implem…
Browse files Browse the repository at this point in the history
…entation (#63315)

* Convert ContextStack to use generics

* Convert AttributeCollection to generics

Also replaced a double dictionary lookup with the `TryGet` variant to
increase performance.

* Convert ComponentResourceManager to generics

* Convert LicInfoHelperLicenseContext to generics

* Convert PropertyDescriptor to generics
  • Loading branch information
MattKotsenas authored Feb 8, 2022
1 parent 829fe59 commit 826896f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace System.ComponentModel
Expand All @@ -19,7 +20,7 @@ public class AttributeCollection : ICollection, IEnumerable
/// </summary>
public static readonly AttributeCollection Empty = new AttributeCollection(null);

private static Hashtable? s_defaultAttributes;
private static Dictionary<Type, Attribute?>? s_defaultAttributes;

private readonly Attribute[] _attributes;

Expand Down Expand Up @@ -247,13 +248,13 @@ public bool Contains(Attribute[]? attributes)
{
if (s_defaultAttributes == null)
{
s_defaultAttributes = new Hashtable();
s_defaultAttributes = new Dictionary<Type, Attribute?>();
}

// If we have already encountered this, use what's in the table.
if (s_defaultAttributes.ContainsKey(attributeType))
if (s_defaultAttributes.TryGetValue(attributeType, out Attribute? defaultAttribute))
{
return (Attribute?)s_defaultAttributes[attributeType];
return defaultAttribute;
}

Attribute? attr = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace System.ComponentModel
/// </summary>
public class ComponentResourceManager : ResourceManager
{
private Hashtable? _resourceSets;
private Dictionary<CultureInfo, SortedList<string, object?>?>? _resourceSets;
private CultureInfo? _neutralResourcesCulture;

public ComponentResourceManager()
Expand Down Expand Up @@ -85,13 +85,13 @@ public virtual void ApplyResources(object value!!, string objectName!!, CultureI

if (_resourceSets == null)
{
_resourceSets = new Hashtable();
_resourceSets = new Dictionary<CultureInfo, SortedList<string, object?>?>();
resources = FillResources(culture, out _);
_resourceSets[culture] = resources;
}
else
{
resources = (SortedList<string, object?>?)_resourceSets[culture];
resources = _resourceSets.GetValueOrDefault(culture, defaultValue: null);
if (resources == null || (resources.Comparer.Equals(StringComparer.OrdinalIgnoreCase) != IgnoreCase))
{
resources = FillResources(culture, out _);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;

namespace System.ComponentModel.Design.Serialization
{
Expand All @@ -23,7 +23,7 @@ namespace System.ComponentModel.Design.Serialization
/// </summary>
public sealed class ContextStack
{
private ArrayList? _contextStack;
private List<object>? _contextStack;

/// <summary>
/// Retrieves the current object on the stack, or null
Expand Down Expand Up @@ -98,7 +98,7 @@ public void Append(object context!!)
{
if (_contextStack == null)
{
_contextStack = new ArrayList();
_contextStack = new List<object>();
}
_contextStack.Insert(0, context);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public void Push(object context!!)
{
if (_contextStack == null)
{
_contextStack = new ArrayList();
_contextStack = new List<object>();
}
_contextStack.Add(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;

namespace System.ComponentModel
{
Expand Down Expand Up @@ -64,9 +62,9 @@ public override void SetSavedLicenseKey(Type type, string? key)
// Used from IClassFactory2 when retrieving LicInfo
private sealed class LicInfoHelperLicenseContext : LicenseContext
{
private readonly Hashtable _savedLicenseKeys = new Hashtable();
private readonly Dictionary<string, string> _savedLicenseKeys = new Dictionary<string, string>();

public bool Contains(string assemblyName) => _savedLicenseKeys.Contains(assemblyName);
public bool Contains(string assemblyName) => _savedLicenseKeys.ContainsKey(assemblyName);

public override LicenseUsageMode UsageMode => LicenseUsageMode.Designtime;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

Expand All @@ -15,7 +16,7 @@ public abstract class PropertyDescriptor : MemberDescriptor
internal const string PropertyDescriptorPropertyTypeMessage = "PropertyDescriptor's PropertyType cannot be statically discovered.";

private TypeConverter? _converter;
private Hashtable? _valueChangedHandlers;
private Dictionary<object, EventHandler?>? _valueChangedHandlers;
private object?[]? _editors;
private Type[]? _editorTypes;
private int _editorCount;
Expand Down Expand Up @@ -125,11 +126,11 @@ public virtual void AddValueChanged(object component!!, EventHandler handler!!)
{
if (_valueChangedHandlers == null)
{
_valueChangedHandlers = new Hashtable();
_valueChangedHandlers = new Dictionary<object, EventHandler?>();
}

EventHandler? h = (EventHandler?)_valueChangedHandlers[component];
_valueChangedHandlers[component] = Delegate.Combine(h, handler);
EventHandler? h = _valueChangedHandlers.GetValueOrDefault(component, defaultValue: null);
_valueChangedHandlers[component] = (EventHandler?)Delegate.Combine(h, handler);
}

/// <summary>
Expand Down Expand Up @@ -383,7 +384,7 @@ protected virtual void OnValueChanged(object? component, EventArgs e)
{
if (component != null)
{
((EventHandler?)_valueChangedHandlers?[component])?.Invoke(component, e);
_valueChangedHandlers?.GetValueOrDefault(component, defaultValue: null)?.Invoke(component, e);
}
}

Expand All @@ -394,7 +395,7 @@ public virtual void RemoveValueChanged(object component!!, EventHandler handler!
{
if (_valueChangedHandlers != null)
{
EventHandler? h = (EventHandler?)_valueChangedHandlers[component];
EventHandler? h = _valueChangedHandlers.GetValueOrDefault(component, defaultValue: null);
h = (EventHandler?)Delegate.Remove(h, handler);
if (h != null)
{
Expand All @@ -416,7 +417,7 @@ public virtual void RemoveValueChanged(object component!!, EventHandler handler!
{
if (component != null && _valueChangedHandlers != null)
{
return (EventHandler?)_valueChangedHandlers[component];
return _valueChangedHandlers.GetValueOrDefault(component, defaultValue: null);
}
else
{
Expand Down

0 comments on commit 826896f

Please sign in to comment.