dotnet/wpf · error · InvalidOperationException
throw new…
Error message
throw new System.InvalidOperationException(SR.Enumerator_VerifyContext);
What it means
WeakReferenceListEnumerator.Current throws InvalidOperationException(SR.Enumerator_VerifyContext) when the strong reference has not been established, meaning MoveNext was not called before reading Current (or the enumerator was used out of order). It enforces the standard IEnumerator protocol.
Solutions
- Call MoveNext() and check it returns true before reading Current
- Restructure as foreach or a while(en.MoveNext()) loop
- Do not read Current again after the loop completes
- Create a fresh enumerator instead of resetting/reusing a finished one
Example fix
// before
var e = list.GetEnumerator();
object v = e.Current;
// after
var e = list.GetEnumerator();
if (e.MoveNext()) { object v = e.Current; } Defensive patterns
Strategy: try-catch
Validate before calling
// protocol check: only read Current after MoveNext()==true // bool canRead = enumerator.MoveNext();
Type guard
static bool TryCurrent(System.Collections.IEnumerator e, out object v) {
v = null;
if (!e.MoveNext()) return false;
v = e.Current; return true;
} Try / catch
try { v = e.Current; }
catch (InvalidOperationException) { /* MoveNext not called/failed: reset loop */ } Prevention
- Always gate Current on MoveNext()'s return value
- Prefer foreach over manual enumerator loops
- Never reuse exhausted enumerators
When it happens
Trigger: Accessing the Current property of WeakReferenceListEnumerator immediately after creation or after MoveNext returned false, without a successful MoveNext establishing _StrongReference.
Common situations: Manual foreach-equivalent loops over weak-reference lists where MoveNext's return value is ignored; resuming an exhausted enumerator; sharing one enumerator across threads.
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.Enumerator_CollectionChanged
- SR.Enumerator_NotStarted
- SR.Enumerator_NotStarted
- SR.Enumerator_NotStarted
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/69dfb790a2a42a54.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/WeakReferenceEnumerator.cs:37
internal struct WeakReferenceListEnumerator : IEnumerator
{
public WeakReferenceListEnumerator( ArrayList List)
{
_i = 0;
_List = List;
_StrongReference = null;
}
object IEnumerator.Current
{ get{ return Current; } }
public object Current
{
get
{
if( null == _StrongReference )
{
throw new System.InvalidOperationException(SR.Enumerator_VerifyContext);
}
return _StrongReference;
}
}
public bool MoveNext()
{
object obj=null;
while( _i < _List.Count )
{
WeakReference weakRef = (WeakReference) _List[ _i++ ];
obj = weakRef.Target;
if(null != obj)
break;
}
_StrongReference = obj;
return (null != obj);
}View on GitHub (pinned to 81131a70a4)