Skip to content

Cannot convert expressions that are for a struct property to non-struct properties #173

Closed
@yinzara

Description

@yinzara

This defect seems to have been introduced in the v12 release.

I think it might actually be caused by:
#144

When attempting to map expressions between properties that are convertible types but one type is a struct and the other is a class, an exception is thrown (instead of the mapping being valid).

ystem.InvalidOperationException : No coercion operator is defined between types 'MyStructType' and 'MyClassType'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
   at AutoMapper.Extensions.ExpressionMapping.Extensions.VisitorExtensions.ConvertTypeIfNecessary(Expression expression, Type memberType)
   at AutoMapper.Extensions.ExpressionMapping.XpressionMapperVisitor.<VisitMember>g__GetMappedMemberExpression|17_0(Expression parentExpression, List`1 propertyMapInfoList, <>c__DisplayClass17_0& )
   at AutoMapper.Extensions.ExpressionMapping.XpressionMapperVisitor.VisitMember(MemberExpression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.<MapExpression>g__MapBody|8_1[TDestDelegate](Dictionary`2 typeMappings, XpressionMapperVisitor visitor, <>c__DisplayClass8_0`1& )
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.<MapExpression>g__CreateVisitor|8_0[TDestDelegate](Dictionary`2 typeMappings, <>c__DisplayClass8_0`1& )
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IConfigurationProvider configurationProvider, LambdaExpression expression, Type typeSourceFunc, Type typeDestFunc, Func`3 getVisitor, Func`2 shouldConvertMappedBodyToDestType)
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression, Func`3 getVisitor, Func`2 shouldConvertMappedBodyToDestType)
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression)

I have created a simple reproduction repository:
https://github.com/yinzara/AutoMapper.Extensions.ExpressionMapping.bug-mapped-types

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