AutoMapper Expression Translation doesn't work

0

AutoMapper Expression Translation example from here http://docs.automapper.org/en/stable/Expression-Translation-(UseAsDataSource).html doesn't work.

I have copied and paste (to make the example as simple as possible) example from AutoMapper site in console app (.net and also .net-core) and add some my own code to check example from the site. But it doesn't work as I expected. It throws exception: System.ArgumentException: 'Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.'

In debugger I see that automapper doesn't change path to property, and it is the problem.

Looks like automapper cannot actually translate expressions or I do it wrong.

public class OrderLine
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Item Item { get; set; }
    public decimal Quantity { get; set; }
}

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class OrderLineDTO
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string Item { get; set; }
    public decimal Quantity { get; set; }
}



class Program
{
    static void Main(string[] args)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<OrderLine, OrderLineDTO>()
              .ForMember(dto => dto.Item, conf => conf.MapFrom(ol => ol.Item.Name));
            cfg.CreateMap<OrderLineDTO, OrderLine>()
              .ForMember(ol => ol.Item, conf => conf.MapFrom(dto => dto));
            cfg.CreateMap<OrderLineDTO, Item>()
              .ForMember(i => i.Name, conf => conf.MapFrom(dto => dto.Item));
        });

        Expression<Func<OrderLineDTO, bool>> dtoExpression = dto => dto.Item.StartsWith("A");
        var expression = Mapper.Map<Expression<Func<OrderLine, bool>>>(dtoExpression);

        //-----------------Code that I have added myself-------------------

        var s = new List<OrderLine>{ new OrderLine{ Item=new Item() { Name="Az"} }, new OrderLine{ Item=new Item() { Name="Bz"} } };

        var dsa = s.Where(expression.Compile());

        //----------------------------------------------------------------
    }
}

System.ArgumentException HResult=0x80070057 Message=Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type. Source=mscorlib StackTrace: at System.Delegate.CreateDelegateNoSecurityCheck(Type type, Object target, RuntimeMethodHandle method) at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target) at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate() at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator) at System.Linq.Expressions.Expression`1.Compile() at TestAutoMap.Net.Program.Main(String[] args) in D:\PRG\Clinch_MVC_Dot_Net_Core\Clinch_MVC_Dot_Net_Core\TestAutoMap.Net\Program.cs:line 56

.net
.net-core
automapper-8
asked on Stack Overflow Jan 5, 2019 by unixqnx

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0