OrchardCMS/OrchardCore · error · ArgumentException

The type must inherit from ContentField

Error message

The type must inherit from ContentField

What it means

Guard inside ContentOptions.GetOrAddContentField: the Type passed to be registered or looked up as a content field does not inherit from the abstract ContentField class. It fires during content field/handler registration (e.g. services.AddContentField<T> or AddFieldHandler) when a developer passes a plain class as T.

Solutions

  1. Ensure the type passed to AddContentField<T> inherits from ContentField.
  2. If you meant to register a part, call AddContentPart<T> instead.
  3. Verify custom fields extend ContentField (e.g. TextField, NumericField base).

Example fix

// before
services.AddContentField<MyPart>(); // MyPart : ContentPart

// after
services.AddContentPart<MyPart>();
Defensive patterns

Strategy: validation

Validate before calling

if (!typeof(ContentField).IsAssignableFrom(typeof(T)))
    throw new ArgumentException("T must inherit from ContentField");

Type guard

bool IsContentField(Type t) => typeof(ContentField).IsAssignableFrom(t);

Try / catch

try
{
    services.AddContentField<T>();
}
catch (ArgumentException e)
{
    throw new InvalidOperationException("AddContentField requires a ContentField subclass", e);
}

Prevention

When it happens

Trigger: Calling AddContentField<T> (or GetOrAddContentField directly) with a type that is not a ContentField subclass, e.g. a ContentPart or plain class.

Common situations: Registering a part type with AddContentField by mistake, creating a custom field that extends ContentPart instead of ContentField, wrong generic parameter in Startup.cs.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/a9b07e24adb6eb8c. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.ContentManagement.Abstractions/ContentOptions.cs:69

    }

    internal void AddFieldHandler(Type contentFieldType, Type handlerType)
    {
        var option = GetOrAddContentField(contentFieldType);
        option.AddHandler(handlerType);
    }

    internal void RemoveFieldHandler(Type contentFieldType, Type handlerType)
    {
        var option = GetOrAddContentField(contentFieldType);
        option.RemoveHandler(handlerType);
    }

    internal ContentFieldOption GetOrAddContentField(Type contentFieldType)
    {
        if (!contentFieldType.IsSubclassOf(typeof(ContentField)))
        {
            throw new ArgumentException("The type must inherit from " + nameof(ContentField));
        }

        var option = _contentFields.FirstOrDefault(x => x.Type == contentFieldType);
        if (option == null)
        {
            option = new ContentFieldOption(contentFieldType);
            _contentFields.Add(option);
        }

        return option;
    }
}

View on GitHub (pinned to 4306c0717f)