{"record":{"id":"b0718fa4e2eef496","repo":"PrismLibrary/Prism","slug":"resources-delegatecommandinvalidgenericpayloadtype","errorCode":null,"errorMessage":"Resources.DelegateCommandInvalidGenericPayloadType","messagePattern":"Resources\\.DelegateCommandInvalidGenericPayloadType","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/DelegateCommand{T}.cs","lineNumber":70,"sourceCode":"        /// </summary>\n        /// <param name=\"executeMethod\">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>\n        /// <param name=\"canExecuteMethod\">Delegate to execute when CanExecute is called on the command. This can be null.</param>\n        /// <exception cref=\"ArgumentNullException\">When both <paramref name=\"executeMethod\"/> and <paramref name=\"canExecuteMethod\"/> are <see langword=\"null\" />.</exception>\n        public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)\n            : base()\n        {\n            if (executeMethod == null || canExecuteMethod == null)\n                throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);\n\n            TypeInfo genericTypeInfo = typeof(T).GetTypeInfo();\n\n            // DelegateCommand allows object or Nullable<>.  \n            // note: Nullable<> is a struct so we cannot use a class constraint.\n            if (genericTypeInfo.IsValueType)\n            {\n                if ((!genericTypeInfo.IsGenericType) || (!typeof(Nullable<>).GetTypeInfo().IsAssignableFrom(genericTypeInfo.GetGenericTypeDefinition().GetTypeInfo())))\n                {\n                    throw new InvalidCastException(Resources.DelegateCommandInvalidGenericPayloadType);\n                }\n            }\n\n            _executeMethod = executeMethod;\n            _canExecuteMethod = canExecuteMethod;\n        }\n\n        ///<summary>\n        ///Executes the command and invokes the <see cref=\"Action{T}\"/> provided during construction.\n        ///</summary>\n        ///<param name=\"parameter\">Data used by the command.</param>\n        public void Execute(T parameter)\n        {\n            try\n            {\n                _executeMethod(parameter);\n            }\n            catch (Exception ex)","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/DelegateCommand{T}.cs#L52-L88","documentation":"DelegateCommand<T> only supports payload types that are reference types or nullable value types. The constructor inspects the generic type argument at runtime and throws InvalidCastException if T is a non-nullable value type, because the underlying execute/canExecute delegates cannot be invoked with such payloads.","triggerScenarios":"Declaring DelegateCommand<int>, DelegateCommand<DateTime>, or any DelegateCommand<TValue> where TValue is a struct that is not Nullable<U>; the exception is thrown from the DelegateCommand constructor.","commonSituations":"Developers migrating from parameterless commands try to bind commands to value-type command parameters (e.g. an int id) as in WPF's RoutedCommand; Prism's DelegateCommand does not allow it.","solutions":["Change the generic parameter to the nullable form, e.g. DelegateCommand<int?>","Use the object payload form DelegateCommand<object> and cast inside the execute method","Use a reference-type wrapper (class) for the payload instead of a struct"],"exampleFix":"// before\npublic DelegateCommand<int> SelectItemCommand { get; }\n// after\npublic DelegateCommand<int?> SelectItemCommand { get; }","handlingStrategy":"validation","validationCode":"bool payloadOk = !typeof(T).IsValueType || Nullable.GetUnderlyingType(typeof(T)) != null;\nif (!payloadOk) throw new InvalidOperationException($\"{typeof(T)} must be a reference type or Nullable<>\");","typeGuard":"static bool IsValidDelegateCommandPayload<T>() => !typeof(T).IsValueType || Nullable.GetUnderlyingType(typeof(T)) != null;","tryCatchPattern":"try { cmd = new DelegateCommand<T>(execute, canExecute); }\ncatch (InvalidCastException) { cmd = new DelegateCommand<object>(o => execute((T)o), canExecute); }","preventionTips":["Only instantiate DelegateCommand<T> with class types or Nullable<> types","Remember value types cannot satisfy the payload contract — prefer T? ","Use DelegateCommand<object> and cast if you need flexible payloads"],"tags":["prism","generics","nullable-value-type","delegatecommand"],"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"}