{"record":{"id":"07c7538b01610689","repo":"HandyOrg/HandyControl","slug":"the-parameter-must-implement-interface-0","errorCode":null,"errorMessage":"The parameter must implement interface {0}.","messagePattern":"The parameter must implement interface (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Shared/Microsoft.Windows.Shell/Standard/Verify.cs","lineNumber":239,"sourceCode":"        }\n    }\n\n    [SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n    [DebuggerStepThrough]\n    internal static void ImplementsInterface(object parameter, Type interfaceType, string parameterName)\n    {\n        bool flag = false;\n        foreach (Type left in parameter.GetType().GetInterfaces())\n        {\n            if (left == interfaceType)\n            {\n                flag = true;\n                break;\n            }\n        }\n        if (!flag)\n        {\n            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, \"The parameter must implement interface {0}.\", new object[]\n            {\n                interfaceType.ToString()\n            }), parameterName);\n        }\n    }\n}\n","sourceCodeStart":221,"sourceCodeEnd":246,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/Standard/Verify.cs#L221-L246","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Implement the missing interface on the class, or pass a compatible instance","Pre-check with requiredInterface.IsAssignableFrom(obj.GetType())","Cast to the interface explicitly first ((IRequired)obj) so the failure surfaces early with a clearer InvalidCastException"],"exampleFix":"// before\napi.Register(new PlainObject()); // PlainObject lacks IBehavior\n// after\npublic class PlainObject : IBehavior { /* ... */ }\n// or guard:\nif (typeof(IBehavior).IsAssignableFrom(obj.GetType())) api.Register(obj);","handlingStrategy":"type-guard","validationCode":"if (obj == null) throw new ArgumentNullException(nameof(obj));\nif (!typeof(IRequired).IsInstanceOfType(obj)) throw new ArgumentException($\"{obj.GetType()} must implement {typeof(IRequired)}\");","typeGuard":"bool ImplementsIface<TIface>(object o) where TIface : class => o is TIface;","tryCatchPattern":"try { api.Register(obj); } catch (ArgumentException ex) when (ex.Message.Contains(\"must implement interface\")) { /* supply a compatible instance */ }","preventionTips":["Use generic constraints (where T : IRequired) so this fails at compile time","Check `is` before passing interface-dependent objects","Ensure DI/test stubs implement all required interfaces"],"tags":["type-check","interface","argument-validation"],"backgroundTag":"type-mismatch","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}