{"record":{"id":"625799ebf3ca2bd7","repo":"OrchardCMS/OrchardCore","slug":"the-type-must-inherit-from-contentpart-contentdisplayoptions","errorCode":null,"errorMessage":"The type must inherit from ContentPart","messagePattern":"The type must inherit from ContentPart","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentDisplayOptions.cs","lineNumber":56,"sourceCode":"        option.ForEditor(editorDriverType, predicate);\n    }\n\n    internal void RemoveContentPartDisplayDriver(Type contentPartType, Type driverType)\n    {\n        if (!typeof(IContentPartDisplayDriver).IsAssignableFrom(driverType))\n        {\n            throw new ArgumentException(\"The type must implement \" + nameof(IContentPartDisplayDriver));\n        }\n\n        var option = GetOrAddContentPartDisplayOption(contentPartType);\n        option.RemoveDisplayDriver(driverType);\n    }\n\n    private ContentPartDisplayOption GetOrAddContentPartDisplayOption(Type contentPartType)\n    {\n        if (!contentPartType.IsSubclassOf(typeof(ContentPart)))\n        {\n            throw new ArgumentException(\"The type must inherit from \" + nameof(ContentPart));\n        }\n\n        var option = _contentParts.FirstOrDefault(x => x.Type == contentPartType);\n        if (option == null)\n        {\n            option = new ContentPartDisplayOption(contentPartType);\n            _contentParts.Add(option);\n        }\n\n        return option;\n    }\n\n    internal void ForContentFieldDisplayMode(Type contentFieldType, Type displayModeDriverType, Func<string, bool> predicate)\n    {\n        if (!typeof(IContentFieldDisplayDriver).IsAssignableFrom(displayModeDriverType))\n        {\n            throw new ArgumentException(\"The type must implement \" + nameof(IContentFieldDisplayDriver));\n        }","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentDisplayOptions.cs#L38-L74","documentation":"GetOrAddContentPartDisplayOption creates or fetches the display option record for a content part type and requires the type to be a subclass of ContentPart. This is called by every part option API (display mode, editor, removal), so any part-type argument that is not an actual ContentPart-derived class triggers this error.","triggerScenarios":"Calling any ForContentPart* / RemoveContentPartDisplayDriver API with contentPartType set to a field type, an interface, the content item type, or a non-ContentPart class","commonSituations":"Swapping part and field types in placement code; passing typeof(ContentPart) itself vs a concrete part; copy-paste where the field type stayed while the rest of the line was changed to a part API","solutions":["Pass the concrete part class (e.g. typeof(ProductPart)) that inherits ContentPart","Guard with typeof(ContentPart).IsAssignableFrom(partType) before calling","If you are configuring a field, use the content-field display option APIs instead"],"exampleFix":"// before\noptions.ForContentPartDisplayMode(typeof(ProductField), driver, d => d == \"Detail\");\n// after\noptions.ForContentPartDisplayMode(typeof(ProductPart), driver, d => d == \"Detail\");","handlingStrategy":"type-guard","validationCode":"if (!typeof(ContentPart).IsAssignableFrom(partType))\n    throw new ArgumentException($\"{partType.Name} does not inherit ContentPart.\");","typeGuard":"static bool IsContentPart(Type t) => typeof(ContentPart).IsAssignableFrom(t);","tryCatchPattern":"try\n{\n    options.ForContentPartDisplayMode(partType, driverType, predicate);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(nameof(ContentPart)))\n{\n    _logger.LogError(ex, \"{Type} is not a ContentPart subclass\", partType.Name);\n}","preventionTips":["Double-check you are passing the part type, not the field type, when using ForContentPart* APIs","Use nameof(YourPart) style references (typeof(ProductPart)) directly from the part class definition","For fields, consistently use the ForContentField* APIs to avoid cross-wiring types"],"tags":["display-management","content-part","type-validation"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}