AutoMapper: Expression of type 'System.Object' cannot be used for assignment to type 'System.String'

I’m having a problem when upgrading from AutoMapper version 3.2.1 to 5.1.1 on a mapping that maps between an ISet<string> to a THashSet<string> object.

We use thrift in our project, and unfortunately the current version we’re using doesn’t have support for the built in ISet<T>. Because of that we’ve got a custom mapping that maps between an ISet<T> and a THashSet<T>.

When we do this, it’s causing the following exception when configuring the mapper:

System.ArgumentException : Expression of type 'System.Object' cannot be used for assignment to type 'System.String'
   at System.Linq.Expressions.Expression.Assign(Expression left, Expression right)
   at AutoMapper.ExpressionExtensions.ForEach(Expression collection, ParameterExpression loopVar, Expression loopContent)
   at AutoMapper.Mappers.CollectionMapperExtensions.MapCollectionExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression, Func`2 conditionalExpression, Type ifInterfaceType, MapItem mapItem)
   at AutoMapper.Mappers.CollectionMapper.MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
   at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, TypePair typePair, Expression sourceParameter, Expression contextParameter, PropertyMap propertyMap, Expression destinationParameter)
   at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression(TypePair typePair, Expression sourceParameter, PropertyMap propertyMap, Expression destinationParameter)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(PropertyMap propertyMap)
   at AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap(PropertyMap propertyMap)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(Expression destinationFunc, Boolean constructorMapping)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda()
   at AutoMapper.TypeMap.Seal(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider)
   at AutoMapper.MapperConfiguration.Seal(IConfiguration configuration)
   at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression, IEnumerable`1 mappers)
   at AutoMapper.MapperConfiguration..ctor(Action`1 configure, IEnumerable`1 mappers)
   at AutoMapper.MapperConfiguration..ctor(Action`1 configure)
   at RD.Api.Tests.Unit.Infrastructure.AutoMapperTests.TestAllMappingConfigurationsAreValid() in C:\Users\AdamConnelly\github.com\adam-resdiary\ResDiary-API\Tests\RD.Api.Tests\Unit\Infrastructure\AutoMapperTests.cs:line 28

I’ve managed to narrow this down to the following code that seems to reproduce the issue:

[TestFixture]
public class MapperTests
{
    [Test]
    public void TestAllMappingConfigurationsAreValid()
    {
        // Exception is thrown by the following line:
        var mapperConfig =
            new MapperConfiguration(
                cfg => cfg.CreateMap<MapFrom, MapTo>().ForMember(dst => dst.Set, o => o.MapFrom(src => MapSet(src.Set))));
    }

    public class MapFrom
    {
        public ISet<string> Set { get; set; }
    }

    public class MapTo
    {
        public THashSet<string> Set { get; set; }
    }

    private static THashSet<string> MapSet(ISet<string> src)
    {
        return null;
    }
}

In our actual code we’re using a Mapper.Initialize() call, but creating a new MapperConfiguration seems to recreate the issue.

I think I can work around this for now by switching to a customer TypeConverter, and using ConvertUsing, which is probably more correct for what we’re trying to do anyway.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

I think we could link from the ReadMe to a page from the wiki.