PrismLibrary/Prism · error · ArgumentException

Resources.InvalidDelegateRerefenceTypeException (formatted…

Error message

Resources.InvalidDelegateRerefenceTypeException (formatted with typeof(Predicate<TPayload>).FullName)

What it means

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.

Solutions

  1. Cast or declare the filter explicitly as Predicate<TPayload> before wrapping it in the DelegateReference
  2. Ensure the payload type of the filter matches the PubSubEvent<TPayload> exactly
  3. Use the Subscribe(action, filter) overload of PubSubEvent and let Prism wrap the delegate for you

Example fix

// before
Func<string, bool> filter = s => s.Length > 3;
var sub = new EventSubscription<string>(actionRef, new DelegateReference(filter, false));
// after
Predicate<string> filter = s => s.Length > 3;
var sub = new EventSubscription<string>(actionRef, new DelegateReference(filter, false));
Defensive patterns

Strategy: type-guard

Validate before calling

if (filterReference?.Target is Predicate<TPayload> filter)
    /* safe to build subscription */;

Type guard

bool IsPredicateOf<TPayload>(IDelegateReference r) => r?.Target is Predicate<TPayload>;

Try / catch

try
{
    var sub = new EventSubscription<string>(actionRef, filterRef);
}
catch (ArgumentException ex) when (ex.ParamName == "filterReference")
{
    // log: filter is not a Predicate<TPayload>
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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 PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/8eae47d5a36c017f. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Events/EventSubscription.cs:113

        ///<summary>
        /// Creates a new instance of <see cref="EventSubscription{TPayload}"/>.
        ///</summary>
        ///<param name="actionReference">A reference to a delegate of type <see cref="System.Action{TPayload}"/>.</param>
        ///<param name="filterReference">A reference to a delegate of type <see cref="Predicate{TPayload}"/>.</param>
        ///<exception cref="ArgumentNullException">When <paramref name="actionReference"/> or <see paramref="filterReference"/> are <see langword="null" />.</exception>
        ///<exception cref="ArgumentException">When the target of <paramref name="actionReference"/> is not of type <see cref="System.Action{TPayload}"/>,
        ///or the target of <paramref name="filterReference"/> is not of type <see cref="Predicate{TPayload}"/>.</exception>
        public EventSubscription(IDelegateReference actionReference, IDelegateReference filterReference)
        {
            if (actionReference == null)
                throw new ArgumentNullException(nameof(actionReference));
            if (!(actionReference.Target is Action<TPayload>))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Action<TPayload>).FullName), nameof(actionReference));

            if (filterReference == null)
                throw new ArgumentNullException(nameof(filterReference));
            if (!(filterReference.Target is Predicate<TPayload>))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidDelegateRerefenceTypeException, typeof(Predicate<TPayload>).FullName), nameof(filterReference));

            _actionReference = actionReference;
            _filterReference = filterReference;
        }

        /// <summary>
        /// Gets the target <see cref="System.Action{T}"/> that is referenced by the <see cref="IDelegateReference"/>.
        /// </summary>
        /// <value>An <see cref="System.Action{T}"/> or <see langword="null" /> if the referenced target is not alive.</value>
        public Action<TPayload> Action
        {
            get { return (Action<TPayload>)_actionReference.Target; }
        }

        /// <summary>
        /// Gets the target <see cref="Predicate{T}"/> that is referenced by the <see cref="IDelegateReference"/>.
        /// </summary>
        /// <value>An <see cref="Predicate{T}"/> or <see langword="null" /> if the referenced target is not alive.</value>

View on GitHub (pinned to 358118cd64)