从 WeakDictionary 获取值时,ItemPeersStorage 如何抛出异常?

How can ItemPeersStorage throw exception when getting value from WeakDictionary?

我通过自动异常报告得到了以下异常,所以我没有太多上下文。看起来该应用程序显示的视图几乎没有控件,DataGrid 是唯一的 ItemsControl。该应用程序使用触摸屏,我假设 tabtip.exe 可能会导致调用 AutomationPeer,因为我没有明确使用任何 UI 自动化。异常似乎是在至少 30 分钟没有任何用户交互的情况下发生的。

我的目标是 .NET Framework 4.7.2,但计算机安装了 .NET Framework 4.8。

有人看过这个吗?关于为什么会发生这种情况的任何提示?

Unhandled exception occurred. Origin=System.Windows.Dispatcher.CurrentDispatcher.UnhandledException:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at MS.Internal.WeakDictionary`2.get_Item(TKey key)
   at System.Windows.Automation.Peers.ItemPeersStorage`1.get_Item(Object item)
   at System.Windows.Automation.Peers.ItemsControlAutomationPeer.GetPeerFromWeakRefStorage(Object item)
   at System.Windows.Automation.Peers.ItemsControlAutomationPeer.AddProxyToWeakRefStorage(WeakReference wr, ItemAutomationPeer itemPeer)
   at System.Windows.Automation.Peers.ItemAutomationPeer.AddToParentProxyWeakRefCache()
   at MS.Internal.Automation.ElementProxy.StaticWrap(AutomationPeer peer, AutomationPeer referencePeer)
   at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
   at System.Windows.Automation.Peers.AutomationPeer.UpdateChildren()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.ContextLayoutManager.fireAutomationEvents()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

来自 .NET Framework 4.8 的 System.Windows.Automation.Peers.ItemPeersStorage1.get_Item(Object item) 方法 Reference Source 我不明白这是怎么发生的?它在访问 WeakDictionary.

之前检查 ContainsKey()
public T this[object item]
{
    get
    {
        if (_count == 0 || item == null)
            return default(T);

        if (_usesHashCode)
        {
            if (_hashtable == null || !_hashtable.ContainsKey(item))
                return default(T);

            return _hashtable[item] as T;
        }
        else
        {
            if (_list == null)
                return default(T);

            for (int i = 0; i < _list.Count; i++)
            {
                KeyValuePair<object, T> pair = _list[i];
                if (Object.Equals(item, pair.Key))
                    return pair.Value;
            }

            return default(T);
        }
}

请查看 ItemPeersStorage 内部使用的 WeakDictionary Source,您会注意到它会针对未包含的键抛出异常。

public TValue this[TKey key]
    {
        get
        {
            if (!_hashTable.ContainsKey(key))
            {
                throw new KeyNotFoundException();
            }
            return (TValue)_hashTable[key];
        }
        set
        {
            _hashTable.SetWeak(key, value);
        }
    }

我下载了 .NET Framework 4.7.2 from Reference Source 的代码,那个版本似乎有一个错误,因为他们没有检查 ContainsKey():

get
{
    if (_count == 0 || item == null)
        return default(T);

    if (_usesHashCode)
    {
        if (_hashtable == null)
            return default(T);

        return _hashtable[item] as T;
    }
    else
    {
        if (_list == null)
            return default(T);

        for (int i = 0; i < _list.Count; i++)
        {
            KeyValuePair<object, T> pair = _list[i];
            if (Object.Equals(item, pair.Key))
                return pair.Value;
        }

        return default(T);
    }
}

因为我的目标是 .NET Framework 4.7.2 并使用 <supportedRuntime/> 配置元素,所以我没有使用 .NET Framework 4.8 代码,这是我最初假设的。

因此,.NET Framework 4.8 中修复了错误。我想我会改用那个版本。