Skip to content

Exception thrown when mapping object inheriting from List<int> #4061

@AlbertoPa

Description

@AlbertoPa

I am trying to map between the two following types:

Source/destination types

    public class Face : List<int>
    {
        public Face() : base(4)
        { }

        public Face(int v0, int v1, int v2, int v3) : base(4)
        {
            Add(v0);
            Add(v1);
            Add(v2);
            Add(v3);
        }

        /// Omitted code here because not relevant
    }
    public class FaceDTO: List<int>
    {
        public FaceDTO() : base(4)
        { }

        public FaceDTO(int v0, int v1, int v2, int v3) : base(4)
        {
            Add(v0);
            Add(v1);
            Add(v2);
            Add(v3);
        }
    }

Mapping configuration

Neither

CreateMap<Face, FaceDTOl>().ForMember("Item", opt => opt.Ignore());
CreateMap<FaceDTO, Face>().ForMember("Item", opt => opt.Ignore());

nor (from #3597)

CreateMap<Face, FaceDTOl>().ForMember("Item", opt => opt.Ignore());
CreateMap<FaceDTO, Face>().ForMember("Item", opt => opt.Ignore());

work.

Version: 11.0.1 (on .Net 6)

Expected behavior

The map definition works and does not disrupt other maps.

Actual behavior

As soon as the mapper is invoked, an exception is thrown:

System.ArgumentException: Incorrect number of arguments supplied for call to method 'FaceDTO get_Item(Int32)' (Parameter 'property')
   at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(MemberMap memberMap, Expression destination, MemberInfo destinationMember)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(Expression createDestination)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda(HashSet`1 typeMapsPath)
   at AutoMapper.TypeMap.Seal(IGlobalConfiguration configurationProvider, HashSet`1 typeMapsPath)
   at AutoMapper.Execution.ExpressionBuilder.MapExpression(IGlobalConfiguration configurationProvider, ProfileMap profileMap, TypePair typePair, Expression sourceParameter, MemberMap propertyMap, Expression destinationParameter)
   at AutoMapper.Execution.TypeMapPlanBuilder.MapMember(MemberMap memberMap, Expression destinationMemberValue, ParameterExpression resolvedValue)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(MemberMap memberMap, Expression destination, MemberInfo destinationMember)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(Expression createDestination)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda(HashSet`1 typeMapsPath)
   at AutoMapper.TypeMap.Seal(IGlobalConfiguration configurationProvider, HashSet`1 typeMapsPath)
   at AutoMapper.MapperConfiguration.Seal()
   at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.<>c.<AddAutoMapperClasses>b__12_2(IServiceProvider sp)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)

Steps to reproduce

Define those two objects, add the mapping configuration above to the AutoMapper profile, start the application and invoke any mapping function, also not related to the types in question.

Thank you for any insight.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions