{"record":{"id":"4a21f54b6442dec1","repo":"LuckyPennySoftware/AutoMapper","slug":"derivedtype-is-not-derived-from-basetype","errorCode":null,"errorMessage":"{derivedType} is not derived from {baseType}.","messagePattern":"(.+?) is not derived from (.+?)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Internal/TypeExtensions.cs","lineNumber":26,"sourceCode":"    public const BindingFlags StaticFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;\n\n    public static MethodInfo StaticGenericMethod(this Type type, string methodName, int parametersCount)\n    {\n        foreach (MethodInfo foundMethod in type.GetMember(methodName, MemberTypes.Method, StaticFlags & ~BindingFlags.NonPublic))\n        {\n            if (foundMethod.IsGenericMethodDefinition && foundMethod.GetParameters().Length == parametersCount)\n            {\n                return foundMethod;\n            }\n        }\n        throw new ArgumentOutOfRangeException(nameof(methodName), $\"Cannot find suitable method {type}.{methodName}({parametersCount} parameters).\");\n    }\n\n    public static void CheckIsDerivedFrom(this Type derivedType, Type baseType)\n    {\n        if (!baseType.IsAssignableFrom(derivedType) && !derivedType.IsGenericTypeDefinition && !baseType.IsGenericTypeDefinition)\n        {\n            throw new ArgumentOutOfRangeException(nameof(derivedType), $\"{derivedType} is not derived from {baseType}.\");\n        }\n    }\n\n    public static bool IsDynamic(this Type type) => typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type);\n\n    public static IEnumerable<Type> BaseClassesAndInterfaces(this Type type)\n    {\n        var currentType = type;\n        while ((currentType = currentType.BaseType) != null)\n        {\n            yield return currentType;\n        }\n        foreach (var interfaceType in type.GetInterfaces())\n        {\n            yield return interfaceType;\n        }\n    }\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Internal/TypeExtensions.cs#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"Wrong generic type arguments in Include or IncludeBase; refactoring that broke an inheritance hierarchy; passing unrelated types when setting up polymorphic mappings.","solutions":["Verify that the Include/IncludeBase/As type arguments have a correct inheritance relationship matching the CreateMap's source and destination types.","For Include<TDerivedSource, TDerivedDest>(), TDerivedSource must derive from the map's source and TDerivedDest from the map's destination.","For IncludeBase<TSourceBase, TDestBase>(), TSourceBase must be a base of the map's source and TDestBase a base of the map's destination.","Fix the CreateMap generic type parameters if they were declared incorrectly."],"exampleFix":"// before — unrelated types in Include\nCreateMap<Source, Dest>().Include<OtherSource, OtherDest>(); // OtherDest not derived from Dest\n\n// after — correct inheritance relationship\npublic class DerivedSource : Source { }\npublic class DerivedDest : Dest { }\nCreateMap<Source, Dest>().Include<DerivedSource, DerivedDest>();\nCreateMap<DerivedSource, DerivedDest>();","handlingStrategy":"validation","validationCode":"// Validate inheritance relationships before calling Include/IncludeBase/As\nstatic void ValidateInclude(Type mapSource, Type mapDest, Type includeSource, Type includeDest)\n{\n    if (!mapSource.IsAssignableFrom(includeSource))\n        throw new ArgumentException($\"{includeSource.Name} must derive from {mapSource.Name}.\");\n    if (!mapDest.IsAssignableFrom(includeDest))\n        throw new ArgumentException($\"{includeDest.Name} must derive from {mapDest.Name}.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always verify that Include/IncludeBase type arguments form a valid inheritance chain with the CreateMap types.","Use generic overloads with constraints where possible for compile-time checking.","Write a unit test that calls AssertConfigurationIsValid to catch hierarchy errors at test time."],"tags":["configuration","polymorphism","inheritance","include"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}