Skip to content

Commit fa0d976

Browse files
authored
Replace Hashtable with Dictionary<> in AxContainer (dotnet#6899)
More strict about types used in the app.
1 parent 54f280c commit fa0d976

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/AxHost.AxContainer.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ internal class AxContainer : IOleContainer, IOleInPlaceFrame, IReflect
2929
private AxHost _siteUIActive;
3030
private AxHost _siteActive;
3131
private bool _formAlreadyCreated;
32-
private readonly Hashtable _containerCache = new(); // name -> Control
32+
private readonly Dictionary<Control, Control> _containerCache = new(); // name -> Control
3333
private int _lockCount;
34-
private Hashtable _components; // Control -> any
35-
private Hashtable _proxyCache;
34+
private Dictionary<Control, Control> _components; // Control -> any
35+
private Dictionary<Control, Oleaut32.IExtender> _proxyCache;
3636
private AxHost _controlInEditMode;
3737

3838
internal AxContainer(ContainerControl parent)
@@ -107,12 +107,12 @@ object IReflect.InvokeMember(
107107
CultureInfo culture,
108108
string[] namedParameters)
109109
{
110-
foreach (DictionaryEntry e in _containerCache)
110+
foreach (var (keyControl, valueControl) in _containerCache)
111111
{
112-
string ctlName = GetNameForControl((Control)e.Key);
112+
string ctlName = GetNameForControl(keyControl);
113113
if (ctlName.Equals(name))
114114
{
115-
return GetProxyForControl((Control)e.Value);
115+
return GetProxyForControl(valueControl);
116116
}
117117
}
118118

@@ -132,11 +132,11 @@ internal Oleaut32.IExtender GetProxyForControl(Control ctl)
132132
Oleaut32.IExtender rval = null;
133133
if (_proxyCache is null)
134134
{
135-
_proxyCache = new Hashtable();
135+
_proxyCache = new();
136136
}
137137
else
138138
{
139-
rval = (Oleaut32.IExtender)_proxyCache[ctl];
139+
_proxyCache.TryGetValue(ctl, out rval);
140140
}
141141

142142
if (rval is null)
@@ -177,7 +177,7 @@ internal void AddControl(Control ctl)
177177
{
178178
lock (this)
179179
{
180-
if (_containerCache.Contains(ctl))
180+
if (_containerCache.ContainsKey(ctl))
181181
{
182182
throw new ArgumentException(string.Format(SR.AXDuplicateControl, GetNameForControl(ctl)), nameof(ctl));
183183
}
@@ -214,7 +214,7 @@ internal void RemoveControl(Control ctl)
214214
{
215215
lock (this)
216216
{
217-
if (_containerCache.Contains(ctl))
217+
if (_containerCache.ContainsKey(ctl))
218218
{
219219
_containerCache.Remove(ctl);
220220
}
@@ -317,7 +317,7 @@ internal IEnumUnknown EnumControls(Control ctl, OLECONTF dwOleContF, GC_WCH dwWh
317317

318318
break;
319319
case GC_WCH.ALL:
320-
Hashtable htbl = GetComponents();
320+
Dictionary<Control, Control> htbl = GetComponents();
321321
ctls = new Control[htbl.Keys.Count];
322322
htbl.Keys.CopyTo(ctls, 0);
323323
ctl = _parent;
@@ -400,12 +400,12 @@ private void FillComponentsTable(IContainer container)
400400
ComponentCollection comps = container.Components;
401401
if (comps is not null)
402402
{
403-
_components = new Hashtable();
403+
_components = new();
404404
foreach (IComponent comp in comps)
405405
{
406-
if (comp is Control && comp != _parent && comp.Site is not null)
406+
if (comp is Control control && comp != _parent && comp.Site is not null)
407407
{
408-
_components.Add(comp, comp);
408+
_components.Add(control, control);
409409
}
410410
}
411411

@@ -423,13 +423,13 @@ private void FillComponentsTable(IContainer container)
423423
{
424424
if (ctls.Length > 0 && _components is null)
425425
{
426-
_components = new Hashtable();
426+
_components = new();
427427
checkHashTable = false;
428428
}
429429

430430
for (int i = 0; i < ctls.Length; i++)
431431
{
432-
if (checkHashTable && !_components.Contains(ctls[i]))
432+
if (checkHashTable && !_components.ContainsKey(ctls[i]))
433433
{
434434
_components.Add(ctls[i], ctls[i]);
435435
}
@@ -448,10 +448,10 @@ private void GetAllChildren(Control ctl)
448448

449449
if (_components is null)
450450
{
451-
_components = new Hashtable();
451+
_components = new();
452452
}
453453

454-
if (ctl != _parent && !_components.Contains(ctl))
454+
if (ctl != _parent && !_components.ContainsKey(ctl))
455455
{
456456
_components.Add(ctl, ctl);
457457
}
@@ -462,12 +462,12 @@ private void GetAllChildren(Control ctl)
462462
}
463463
}
464464

465-
private Hashtable GetComponents()
465+
private Dictionary<Control, Control> GetComponents()
466466
{
467467
return GetComponents(GetParentsContainer());
468468
}
469469

470-
private Hashtable GetComponents(IContainer cont)
470+
private Dictionary<Control, Control> GetComponents(IContainer cont)
471471
{
472472
if (_lockCount == 0)
473473
{
@@ -479,8 +479,8 @@ private Hashtable GetComponents(IContainer cont)
479479

480480
private bool GetControlBelongs(Control ctl)
481481
{
482-
Hashtable comps = GetComponents();
483-
return comps[ctl] is not null;
482+
Dictionary<Control, Control> comps = GetComponents();
483+
return comps.ContainsKey(ctl);
484484
}
485485

486486
private IContainer GetParentIsDesigned()
@@ -628,7 +628,7 @@ internal void OnUIActivate(AxHost site)
628628

629629
private void ListAxControls(ArrayList list, bool fuseOcx)
630630
{
631-
Hashtable components = GetComponents();
631+
var components = GetComponents();
632632
if (components is null)
633633
{
634634
return;

0 commit comments

Comments
 (0)