OrchardCMS/OrchardCore · error · ArgumentException

The type must inherit from ContentPart

Error message

The type must inherit from ContentPart

What it means

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.

Solutions

  1. Pass the concrete part class (e.g. typeof(ProductPart)) that inherits ContentPart
  2. Guard with typeof(ContentPart).IsAssignableFrom(partType) before calling
  3. If you are configuring a field, use the content-field display option APIs instead

Example fix

// before
options.ForContentPartDisplayMode(typeof(ProductField), driver, d => d == "Detail");
// after
options.ForContentPartDisplayMode(typeof(ProductPart), driver, d => d == "Detail");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!typeof(ContentPart).IsAssignableFrom(partType))
    throw new ArgumentException($"{partType.Name} does not inherit ContentPart.");

Type guard

static bool IsContentPart(Type t) => typeof(ContentPart).IsAssignableFrom(t);

Try / catch

try
{
    options.ForContentPartDisplayMode(partType, driverType, predicate);
}
catch (ArgumentException ex) when (ex.Message.Contains(nameof(ContentPart)))
{
    _logger.LogError(ex, "{Type} is not a ContentPart subclass", partType.Name);
}

Prevention

When it happens

Trigger: Calling any ForContentPart* / RemoveContentPartDisplayDriver API with contentPartType set to a field type, an interface, the content item type, or a non-ContentPart class

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

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 OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/625799ebf3ca2bd7. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentDisplayOptions.cs:56

        option.ForEditor(editorDriverType, predicate);
    }

    internal void RemoveContentPartDisplayDriver(Type contentPartType, Type driverType)
    {
        if (!typeof(IContentPartDisplayDriver).IsAssignableFrom(driverType))
        {
            throw new ArgumentException("The type must implement " + nameof(IContentPartDisplayDriver));
        }

        var option = GetOrAddContentPartDisplayOption(contentPartType);
        option.RemoveDisplayDriver(driverType);
    }

    private ContentPartDisplayOption GetOrAddContentPartDisplayOption(Type contentPartType)
    {
        if (!contentPartType.IsSubclassOf(typeof(ContentPart)))
        {
            throw new ArgumentException("The type must inherit from " + nameof(ContentPart));
        }

        var option = _contentParts.FirstOrDefault(x => x.Type == contentPartType);
        if (option == null)
        {
            option = new ContentPartDisplayOption(contentPartType);
            _contentParts.Add(option);
        }

        return option;
    }

    internal void ForContentFieldDisplayMode(Type contentFieldType, Type displayModeDriverType, Func<string, bool> predicate)
    {
        if (!typeof(IContentFieldDisplayDriver).IsAssignableFrom(displayModeDriverType))
        {
            throw new ArgumentException("The type must implement " + nameof(IContentFieldDisplayDriver));
        }

View on GitHub (pinned to 4306c0717f)