LuckyPennySoftware/AutoMapper · error · InvalidOperationException
As must specify a derived type, not {DestinationType}
Error message
As must specify a derived type, not {DestinationType} What it means
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.
Source
Thrown at src/AutoMapper/Configuration/TypeMapConfiguration.cs:237
{
if (config.DestinationMember == destinationMember)
{
return config;
}
}
return null;
}
protected abstract void IgnoreDestinationMember(MemberInfo property, bool ignorePaths = true);
}
public abstract class MappingExpressionBase<TSource, TDestination, TMappingExpression>(MemberList memberList, TypePair types) : TypeMapConfiguration(memberList, types), IMappingExpressionBase<TSource, TDestination, TMappingExpression>
where TMappingExpression : class, IMappingExpressionBase<TSource, TDestination, TMappingExpression>
{
protected MappingExpressionBase(MemberList memberList) : this(memberList, new(typeof(TSource), typeof(TDestination))) { }
public void As(Type typeOverride)
{
if (typeOverride == DestinationType)
{
throw new InvalidOperationException("As must specify a derived type, not " + DestinationType);
}
typeOverride.CheckIsDerivedFrom(DestinationType);
DestinationTypeOverride = typeOverride;
}
public TMappingExpression MaxDepth(int depth)
{
TypeMapActions.Add(tm => tm.MaxDepth = depth);
return this as TMappingExpression;
}
public TMappingExpression ConstructUsingServiceLocator()
{
TypeMapActions.Add(tm => tm.ConstructUsingServiceLocator());
return this as TMappingExpression;
}
public TMappingExpression BeforeMap(Action<TSource, TDestination> beforeFunction) => BeforeMapCore((src, dest, ctxt) => beforeFunction(src, dest));
private TMappingExpression BeforeMapCore(Expression<Action<TSource, TDestination, ResolutionContext>> expr)View on GitHub (pinned to dfa6dd587c)
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.
Example fix
// before — passing the same type CreateMap<Animal, AnimalDto>().As(typeof(AnimalDto)); // after — passing a derived type, or remove As CreateMap<Animal, AnimalDto>(); CreateMap<Dog, DogDto>().IncludeBase<Animal, AnimalDto>();
Defensive patterns
Strategy: validation
Validate before calling
// Validate that the As type is derived from the destination before calling
Type asType = typeof(DerivedDest);
if (asType == typeof(TDestination))
throw new ArgumentException($"As must specify a derived type, not {asType.Name} itself.");
if (!typeof(TDestination).IsAssignableFrom(asType))
throw new ArgumentException($"{asType.Name} must derive from {typeof(TDestination).Name}."); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Generic helper code that resolves T to the destination type; copy-paste error; misunderstanding that As requires a strictly more derived type.
Related errors
- The type {typeMap.DestinationType.Name} does not have a cons
- Only member accesses are allowed. {destinationMember}
- Type '{conditionType.Name}' does not implement ICondition<TS
- Type '{conditionType.Name}' does not implement IPreCondition
- sourceExpression may not be null when mapping {DestinationMe
AI-assisted analysis of LuckyPennySoftware/AutoMapper@dfa6dd587c (2026-08-13).
Data as JSON: /api/errors/1e45ffd32992604e.
Report an issue: GitHub.