{"record":{"id":"1e45ffd32992604e","repo":"LuckyPennySoftware/AutoMapper","slug":"as-must-specify-a-derived-type-not-destinationty","errorCode":null,"errorMessage":"As must specify a derived type, not {DestinationType}","messagePattern":"As must specify a derived type, not (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Configuration/TypeMapConfiguration.cs","lineNumber":237,"sourceCode":"        {\n            if (config.DestinationMember == destinationMember)\n            {\n                return config;\n            }\n        }\n        return null;\n    }\n    protected abstract void IgnoreDestinationMember(MemberInfo property, bool ignorePaths = true);\n}\npublic abstract class MappingExpressionBase<TSource, TDestination, TMappingExpression>(MemberList memberList, TypePair types) : TypeMapConfiguration(memberList, types), IMappingExpressionBase<TSource, TDestination, TMappingExpression>\n    where TMappingExpression : class, IMappingExpressionBase<TSource, TDestination, TMappingExpression>\n{\n    protected MappingExpressionBase(MemberList memberList) : this(memberList, new(typeof(TSource), typeof(TDestination))) { }\n    public void As(Type typeOverride)\n    {\n        if (typeOverride == DestinationType)\n        {\n            throw new InvalidOperationException(\"As must specify a derived type, not \" + DestinationType);\n        }\n        typeOverride.CheckIsDerivedFrom(DestinationType);\n        DestinationTypeOverride = typeOverride;\n    }\n    public TMappingExpression MaxDepth(int depth)\n    {\n        TypeMapActions.Add(tm => tm.MaxDepth = depth);\n\n        return this as TMappingExpression;\n    }\n    public TMappingExpression ConstructUsingServiceLocator()\n    {\n        TypeMapActions.Add(tm => tm.ConstructUsingServiceLocator());\n\n        return this as TMappingExpression;\n    }\n    public TMappingExpression BeforeMap(Action<TSource, TDestination> beforeFunction) => BeforeMapCore((src, dest, ctxt) => beforeFunction(src, dest));\n    private TMappingExpression BeforeMapCore(Expression<Action<TSource, TDestination, ResolutionContext>> expr)","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Configuration/TypeMapConfiguration.cs#L219-L255","documentation":"Thrown by the As(Type) method when the type argument equals the destination type itself. As is designed to override the destination with a derived type for polymorphic dispatch, so passing the same type is meaningless. After this check, CheckIsDerivedFrom validates that typeOverride actually derives from DestinationType.","triggerScenarios":"Calling .As(typeof(DestinationType)) or .As<TDestination>() where the type argument is identical to the map's destination type. This can happen in generic code where the type parameter resolves to the destination type at runtime.","commonSituations":"Generic helper code that resolves T to the destination type; copy-paste error; misunderstanding that As requires a strictly more derived type.","solutions":["Pass a type that is derived from (a subtype of) the destination type.","If you do not need a type override, remove the As call entirely.","In generic code, add a generic constraint (where T : TDestination) and ensure T is actually more specific."],"exampleFix":"// before — passing the same type\nCreateMap<Animal, AnimalDto>().As(typeof(AnimalDto));\n\n// after — passing a derived type, or remove As\nCreateMap<Animal, AnimalDto>();\nCreateMap<Dog, DogDto>().IncludeBase<Animal, AnimalDto>();","handlingStrategy":"validation","validationCode":"// Validate that the As type is derived from the destination before calling\nType asType = typeof(DerivedDest);\nif (asType == typeof(TDestination))\n    throw new ArgumentException($\"As must specify a derived type, not {asType.Name} itself.\");\nif (!typeof(TDestination).IsAssignableFrom(asType))\n    throw new ArgumentException($\"{asType.Name} must derive from {typeof(TDestination).Name}.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass a strictly more derived type to As().","If you don't need polymorphic dispatch, omit the As call.","Add generic constraints (where TDerived : TDestination) in helper code to enforce the relationship at compile time."],"tags":["configuration","polymorphism","as-override","mapping"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}