LuckyPennySoftware/AutoMapper · error · ArgumentOutOfRangeException

{derivedType} is not derived from {baseType}.

Error message

{derivedType} is not derived from {baseType}.

What it means

Thrown by CheckIsDerivedFrom when two types do not have an inheritance relationship (baseType.IsAssignableFrom(derivedType) is false) and neither is a generic type definition. This method is called from IncludeCore, IncludeBaseCore, and As to validate that derived/base type pairs are actually related by inheritance.

Source

Thrown at src/AutoMapper/Internal/TypeExtensions.cs:26

    public const BindingFlags StaticFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

    public static MethodInfo StaticGenericMethod(this Type type, string methodName, int parametersCount)
    {
        foreach (MethodInfo foundMethod in type.GetMember(methodName, MemberTypes.Method, StaticFlags & ~BindingFlags.NonPublic))
        {
            if (foundMethod.IsGenericMethodDefinition && foundMethod.GetParameters().Length == parametersCount)
            {
                return foundMethod;
            }
        }
        throw new ArgumentOutOfRangeException(nameof(methodName), $"Cannot find suitable method {type}.{methodName}({parametersCount} parameters).");
    }

    public static void CheckIsDerivedFrom(this Type derivedType, Type baseType)
    {
        if (!baseType.IsAssignableFrom(derivedType) && !derivedType.IsGenericTypeDefinition && !baseType.IsGenericTypeDefinition)
        {
            throw new ArgumentOutOfRangeException(nameof(derivedType), $"{derivedType} is not derived from {baseType}.");
        }
    }

    public static bool IsDynamic(this Type type) => typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);

    public static IEnumerable<Type> BaseClassesAndInterfaces(this Type type)
    {
        var currentType = type;
        while ((currentType = currentType.BaseType) != null)
        {
            yield return currentType;
        }
        foreach (var interfaceType in type.GetInterfaces())
        {
            yield return interfaceType;
        }
    }

View on GitHub (pinned to dfa6dd587c)

Solutions

  1. Verify that the Include/IncludeBase/As type arguments have a correct inheritance relationship matching the CreateMap's source and destination types.
  2. For Include<TDerivedSource, TDerivedDest>(), TDerivedSource must derive from the map's source and TDerivedDest from the map's destination.
  3. For IncludeBase<TSourceBase, TDestBase>(), TSourceBase must be a base of the map's source and TDestBase a base of the map's destination.
  4. Fix the CreateMap generic type parameters if they were declared incorrectly.

Example fix

// before — unrelated types in Include
CreateMap<Source, Dest>().Include<OtherSource, OtherDest>(); // OtherDest not derived from Dest

// after — correct inheritance relationship
public class DerivedSource : Source { }
public class DerivedDest : Dest { }
CreateMap<Source, Dest>().Include<DerivedSource, DerivedDest>();
CreateMap<DerivedSource, DerivedDest>();
Defensive patterns

Strategy: validation

Validate before calling

// Validate inheritance relationships before calling Include/IncludeBase/As
static void ValidateInclude(Type mapSource, Type mapDest, Type includeSource, Type includeDest)
{
    if (!mapSource.IsAssignableFrom(includeSource))
        throw new ArgumentException($"{includeSource.Name} must derive from {mapSource.Name}.");
    if (!mapDest.IsAssignableFrom(includeDest))
        throw new ArgumentException($"{includeDest.Name} must derive from {mapDest.Name}.");
}

Prevention

When it happens

Trigger: Calling .Include<DerivedSource, DerivedDest>() where DerivedDest is not a subclass of the map's destination; .IncludeBase<SourceBase, DestBase>() where SourceBase is not a base of the map's source; .As(typeof(X)) where X does not derive from the destination type.

Common situations: Wrong generic type arguments in Include or IncludeBase; refactoring that broke an inheritance hierarchy; passing unrelated types when setting up polymorphic mappings.

Related errors


AI-assisted analysis of LuckyPennySoftware/AutoMapper@dfa6dd587c (2026-08-13). Data as JSON: /api/errors/4a21f54b6442dec1. Report an issue: GitHub.