Skip to content

Error while mapping expression - InvalidOperationException: No coercion operator is defined between types #12

Closed
@Rujith

Description

@Rujith

AddExpressionMapping() not working for Expression with open generic mapping.

Source/destination types

    public class ReportS
    {
        public IList<MenuS<OptionS, Func<OptionS>>> Menu { get; set; }
    }

    public class ReportT
    {
        public IList<MenuT<OptionT, Func<OptionT>>> Menu { get; set; }
    }

    public class MenuT<T, TV>
    {
        public Expression<TV> LambdaExpression { get; set; }
        public T Option { get; set; }
    }

    public class MenuS<T, TV>
    {
        public Expression<TV> LambdaExpression { get; set; }
        public T Option { get; set; }
    }

    public class OptionS
    {
    }

    public class OptionT
    {
    }

Mapping configuration

 Mapper.Initialize(
                cfg =>
                    {
                        cfg.AddExpressionMapping();
                        cfg.CreateMap<OptionS, OptionT>();
                        cfg.CreateMap(typeof(MenuS<,>), typeof(MenuT<,>));
                        cfg.CreateMap<ReportS, ReportT>();
                    });

Version: x.y.z

Automapper 7.0.1
AutoMapper.Extensions.ExpressionMapping 1.0.3

Expected behavior

Expression should have mapped to target type

Actual behavior

InvalidOperationException: No coercion operator is defined between types 'OptionS' and 'OptionT'.

Steps to reproduce

  var source = new ReportS
                             {
                                 Menu = new List<MenuS<OptionS, Func<OptionS>>>
                                            {
                                                new MenuS<OptionS, Func<OptionS>>
                                                    {
                                                        Option = new OptionS(), LambdaExpression = () => new OptionS()
                                                    }
                                            }
                             };
            var target = Mapper.Map<ReportT>(source);

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