HandyOrg/HandyControl · error · ArgumentException

The parameter must implement interface

Error message

The parameter must implement interface {0}.

What it means

A generic helper (in Verify.cs) enumerates the interfaces of an object's type and throws ArgumentException when none of them matches the required interfaceType, formatting the interface's full name into the message. Unlike TypeSupportsInterface (which checks a Type), this operates over an instance's implemented interfaces.

Solutions

  1. Implement the missing interface on the class, or pass a compatible instance
  2. Pre-check with requiredInterface.IsAssignableFrom(obj.GetType())
  3. Cast to the interface explicitly first ((IRequired)obj) so the failure surfaces early with a clearer InvalidCastException

Example fix

// before
api.Register(new PlainObject()); // PlainObject lacks IBehavior
// after
public class PlainObject : IBehavior { /* ... */ }
// or guard:
if (typeof(IBehavior).IsAssignableFrom(obj.GetType())) api.Register(obj);
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj == null) throw new ArgumentNullException(nameof(obj));
if (!typeof(IRequired).IsInstanceOfType(obj)) throw new ArgumentException($"{obj.GetType()} must implement {typeof(IRequired)}");

Type guard

bool ImplementsIface<TIface>(object o) where TIface : class => o is TIface;

Try / catch

try { api.Register(obj); } catch (ArgumentException ex) when (ex.Message.Contains("must implement interface")) { /* supply a compatible instance */ }

Prevention

When it happens

Trigger: Passing an object instance whose runtime type does not implement the required interface to an API that runs this check — e.g. a class missing a needed interface after refactoring.

Common situations: Passing proxy or wrapped objects whose runtime type differs from the intended type; partial interface implementation dropped during refactors; dependency-injected stubs in tests missing interfaces the real class implements.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/Standard/Verify.cs:239

        }
    }

    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    [DebuggerStepThrough]
    internal static void ImplementsInterface(object parameter, Type interfaceType, string parameterName)
    {
        bool flag = false;
        foreach (Type left in parameter.GetType().GetInterfaces())
        {
            if (left == interfaceType)
            {
                flag = true;
                break;
            }
        }
        if (!flag)
        {
            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The parameter must implement interface {0}.", new object[]
            {
                interfaceType.ToString()
            }), parameterName);
        }
    }
}

View on GitHub (pinned to 2c0875ebd6)