dotnet/wpf · error · InvalidOperationException
SR.Enumerator_VerifyContext
Error message
SR.Enumerator_VerifyContext
What it means
The CharacterMetricsDictionary Enumerator's GetCurrentEntry throws this InvalidOperationException when Entry or Current is accessed before the enumerator has successfully advanced to a valid element (MoveNext was never called or returned false). SR.Enumerator_VerifyContext means there is no current entry to return.
Solutions
- Call MoveNext() and check it returns true before reading Entry/Current
- Guard the Entry access with a check that enumeration has started and is positioned on a valid element
- Handle empty dictionaries explicitly before enumerating
Example fix
// before
var e = ((IDictionary)dict).GetEnumerator();
var entry = ((IDictionaryEnumerator)e).Entry;
// after
var e = ((IDictionary)dict).GetEnumerator();
if (e.MoveNext()) { var entry = ((IDictionaryEnumerator)e).Entry; } Defensive patterns
Strategy: try-catch
Validate before calling
bool CanReadEntry(IEnumerator e) => e is IDictionaryEnumerator de && /* enumeration started and positioned */ de.MoveNextCalled(); // track MoveNext yourself
Try / catch
try { var entry = ((IDictionaryEnumerator)enumerator).Entry; } catch (InvalidOperationException) { /* enumerator not positioned; call MoveNext first */ } Prevention
- Always call MoveNext() and check true before reading Entry/Current
- Never read Current on a fresh or Reset enumerator
- Handle empty dictionaries before iterating
When it happens
Trigger: Accessing IDictionaryEnumerator.Entry, Entry.Key/Value, or the KeyValuePair Current immediately after GetEnumerator(), after Reset(), or after MoveNext() returned false (end of collection or empty dictionary).
Common situations: Enumerating an empty CharacterMetricsDictionary and reading Current unconditionally; manual enumerator loops that read Entry before the first MoveNext; iterators that use IDictionaryEnumerator.Entry without guarding.
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
- InvalidOperationException
- SR.CannotChangePublishLicense
- SR.Enumerator_CollectionChanged
- SR.Enumerator_NotStarted
- SR.Enumerator_NotStarted
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/88ce78b32a16c150.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:576
return new SC.DictionaryEntry(entry.Key, entry.Value);
}
}
// Current property for generic enumerator.
public KeyValuePair<int, CharacterMetrics> Current
{
get
{
return new KeyValuePair<int, CharacterMetrics>(_unicodeScalar, _value);
}
}
private KeyValuePair<int, CharacterMetrics> GetCurrentEntry()
{
if (_value != null)
return new KeyValuePair<int, CharacterMetrics>(_unicodeScalar, _value);
else
throw new InvalidOperationException(SR.Enumerator_VerifyContext);
}
SC.DictionaryEntry SC.IDictionaryEnumerator.Entry
{
get
{
KeyValuePair<int, CharacterMetrics> entry = GetCurrentEntry();
return new SC.DictionaryEntry(entry.Key, entry.Value);
}
}
object SC.IDictionaryEnumerator.Key
{
get
{
return GetCurrentEntry().Key;
}
}View on GitHub (pinned to 81131a70a4)