dotnet/wpf · error · InvalidOperationException

SR.Enumerator_VerifyContext

Error message

SR.Enumerator_VerifyContext

What it means

The non-generic enumerator wrapper of CultureSpecificStringDictionary throws InvalidOperationException (Enumerator_VerifyContext) when Current is read while no current item exists. Generic IEnumerator<T> is not required to throw in that case, so the IDictionaryEntry-producing wrapper detects a null Key and surfaces the standard 'enumerator out of context' error.

Solutions

  1. Call MoveNext() and check it returned true before reading Key/Value/Entry.
  2. Restart enumeration with GetEnumerator() after the collection changes.
  3. Enumerate the generic IEnumerable<KeyValuePair<XmlLanguage,string>> surface or use ToArray() to snapshot.

Example fix

// before
var e = ((IDictionary)dict).GetEnumerator();
var key = e.Key; // no MoveNext yet
// after
var e = ((IDictionary)dict).GetEnumerator();
if (e.MoveNext()) { var key = e.Key; }
Defensive patterns

Strategy: validation

Validate before calling

if (!enumerator.MoveNext()) return; // no current entry
var key = enumerator.Key; // safe now

Type guard

static bool HasCurrent(IDictionaryEnumerator e) { try { return e.Key != null; } catch (InvalidOperationException) { return false; } }

Try / catch

try { var k = enumerator.Key; }
catch (InvalidOperationException) { /* enumerator not positioned; call MoveNext first */ }

Prevention

When it happens

Trigger: Calling entry.Key, entry.Value, or entry.Entry on the non-generic IDictionaryEnumerator positioned before MoveNext, after MoveNext returned false, or after the collection was modified (invalidation).

Common situations: Manual foreach over the non-generic IDictionary interface (e.g. from data-binding or scripting engines); reading the current entry before the first MoveNext; collection mutated during enumeration by another thread.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ef521bff7575b7d5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs:393

            // classes to return the Key and Value, respectively.
            public virtual object Current
            {
                get
                {
                    return Entry;
                }
            }

            private KeyValuePair<XmlLanguage, string> GetCurrentEntry()
            {
                // Get the key-value pair from the generic IDictionary.
                KeyValuePair<XmlLanguage, string> entry = _enumerator.Current;

                // If there is no current item a non-generic IEnumerator should throw an exception,
                // but a generic IEnumerator<T> is not required to. Therefore we need to check for
                // this case here by checking for a null Key.
                if (entry.Key == null)
                    throw new InvalidOperationException(SR.Enumerator_VerifyContext);

                return entry;
            }

            public SC.DictionaryEntry Entry
            {
                get
                {
                    KeyValuePair<XmlLanguage, string> entry = GetCurrentEntry();
                    return new SC.DictionaryEntry(entry.Key, entry.Value);
                }
            }

            public object Key
            {
                get
                {
                    return GetCurrentEntry().Key;

View on GitHub (pinned to 81131a70a4)