HandyOrg/HandyControl · error · ObjectDisposedException

Specified cast is not valid.

Error message

Specified cast is not valid.

What it means

InvalidCastException ('Specified cast is not valid.') raised in DisposableObject's disposal path (ThrowIfDisposed/Dispose): an object retrieved during disposing — typically from the disposed-event or resource cleanup — is not the type it is cast to. The generic message indicates the runtime type of a value did not match the target cast inside this disposed-object plumbing.

Solutions

  1. Use 'is' pattern matching or 'as' with a null check instead of a hard cast
  2. Verify the actual runtime type (GetType/inspect) before casting in Dispose overrides
  3. Ensure generic/base disposable state stores consistent types across derived classes
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/Shared/HandyControl_Shared/Data/DisposableObject.cs:37 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/770c6760744cf996. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Data/DisposableObject.cs:37

        Dispose(false);
    }

    public event EventHandler Disposing
    {
        add
        {
            ThrowIfDisposed();
            _disposing += value;
        }
        remove
        {
            _disposing -= value;
        }
    }

    protected void ThrowIfDisposed()
    {
        if (IsDisposed) throw new ObjectDisposedException(GetType().Name);
    }

    protected void Dispose(bool disposing)
    {
        if (IsDisposed) return;

        try
        {
            if (disposing)
            {
                _disposing?.Invoke(this, EventArgs.Empty);
                _disposing = null;
                DisposeManagedResources();
            }

            DisposeNativeResources();
        }
        finally

View on GitHub (pinned to 2c0875ebd6)