{"record":{"id":"8eae47d5a36c017f","repo":"PrismLibrary/Prism","slug":"resources-invaliddelegatererefencetypeexception-formatted-8eae47","errorCode":null,"errorMessage":"Resources.InvalidDelegateRerefenceTypeException (formatted with typeof(Predicate<TPayload>).FullName)","messagePattern":"Resources\\.InvalidDelegateRerefenceTypeException \\(formatted with typeof\\(Predicate<TPayload>\\)\\.FullName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Prism.Events/EventSubscription.cs","lineNumber":113,"sourceCode":"        ///<summary>\n        /// Creates a new instance of <see cref=\"EventSubscription{TPayload}\"/>.\n        ///</summary>\n        ///<param name=\"actionReference\">A reference to a delegate of type <see cref=\"System.Action{TPayload}\"/>.</param>\n        ///<param name=\"filterReference\">A reference to a delegate of type <see cref=\"Predicate{TPayload}\"/>.</param>\n        ///<exception cref=\"ArgumentNullException\">When <paramref name=\"actionReference\"/> or <see paramref=\"filterReference\"/> are <see langword=\"null\" />.</exception>\n        ///<exception cref=\"ArgumentException\">When the target of <paramref name=\"actionReference\"/> is not of type <see cref=\"System.Action{TPayload}\"/>,\n        ///or the target of <paramref name=\"filterReference\"/> is not of type <see cref=\"Predicate{TPayload}\"/>.</exception>\n        public EventSubscription(IDelegateReference actionReference, IDelegateReference filterReference)\n        {\n            if (actionReference == null)\n                throw new ArgumentNullException(nameof(actionReference));\n            if (!(actionReference.Target is Action<TPayload>))\n                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Action<TPayload>).FullName), nameof(actionReference));\n\n            if (filterReference == null)\n                throw new ArgumentNullException(nameof(filterReference));\n            if (!(filterReference.Target is Predicate<TPayload>))\n                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Predicate<TPayload>).FullName), nameof(filterReference));\n\n            _actionReference = actionReference;\n            _filterReference = filterReference;\n        }\n\n        /// <summary>\n        /// Gets the target <see cref=\"System.Action{T}\"/> that is referenced by the <see cref=\"IDelegateReference\"/>.\n        /// </summary>\n        /// <value>An <see cref=\"System.Action{T}\"/> or <see langword=\"null\" /> if the referenced target is not alive.</value>\n        public Action<TPayload> Action\n        {\n            get { return (Action<TPayload>)_actionReference.Target; }\n        }\n\n        /// <summary>\n        /// Gets the target <see cref=\"Predicate{T}\"/> that is referenced by the <see cref=\"IDelegateReference\"/>.\n        /// </summary>\n        /// <value>An <see cref=\"Predicate{T}\"/> or <see langword=\"null\" /> if the referenced target is not alive.</value>","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Events/EventSubscription.cs#L95-L131","documentation":"EventSubscription verifies that filterReference.Target is actually a Predicate<TPayload>. Prism throws ArgumentException with Resources.InvalidDelegateRerefenceTypeException when a delegate of another type (e.g. Action<TPayload> or Func<string,bool> with wrong payload type) was supplied as the filter.","triggerScenarios":"Calling new EventSubscription<TPayload>(actionReference, filterReference) where filterReference wraps a delegate that is not exactly Predicate<TPayload>, e.g. Func<TPayload,bool> or a Predicate<TOther> with a mismatched payload type.","commonSituations":"Wrapping a lambda in DelegateReference without casting to the exact Predicate<TPayload> type; refactoring a payload type so an old filter no longer matches; passing a method group whose inferred type is Func instead of Predicate.","solutions":["Cast or declare the filter explicitly as Predicate<TPayload> before wrapping it in the DelegateReference","Ensure the payload type of the filter matches the PubSubEvent<TPayload> exactly","Use the Subscribe(action, filter) overload of PubSubEvent and let Prism wrap the delegate for you"],"exampleFix":"// before\nFunc<string, bool> filter = s => s.Length > 3;\nvar sub = new EventSubscription<string>(actionRef, new DelegateReference(filter, false));\n// after\nPredicate<string> filter = s => s.Length > 3;\nvar sub = new EventSubscription<string>(actionRef, new DelegateReference(filter, false));","handlingStrategy":"type-guard","validationCode":"if (filterReference?.Target is Predicate<TPayload> filter)\n    /* safe to build subscription */;","typeGuard":"bool IsPredicateOf<TPayload>(IDelegateReference r) => r?.Target is Predicate<TPayload>;","tryCatchPattern":"try\n{\n    var sub = new EventSubscription<string>(actionRef, filterRef);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"filterReference\")\n{\n    // log: filter is not a Predicate<TPayload>\n}","preventionTips":["Declare filter lambdas explicitly as Predicate<TPayload>","Match the payload type exactly to the PubSubEvent<TPayload> generic argument","Let PubSubEvent.Subscribe wrap delegates rather than building DelegateReference yourself"],"tags":["prism","events","type-mismatch","delegate"],"backgroundTag":"type-mismatch","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}