Reputation: 6047
I have read AutoMapping Array to a List but this is actually List to Array and I do want Array to List. Here is my code:
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PocArrayListMap
{
class Destination
{
public List<string> list = new List<string>();
}
class Origin
{
public string[] array;
}
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => cfg.CreateMap<Origin, Destination>().ForMember(e => e.list, opts => opts.MapFrom(s => s.array.ToList())));
var config = new MapperConfiguration(cfg => cfg.CreateMap<Origin, Destination>());
config.AssertConfigurationIsValid(); // Exception here
var o = new Origin() { array = new string[] { "one", "two", "three" } };
var d = Mapper.Map<Destination>(o);
Console.WriteLine(string.Join(", ", d.list));
}
}
}
and here is the exception message:
Origin -> Destination (Destination member list)
PocArrayListMap.Origin -> PocArrayListMap.Destination (Destination member list)
Unmapped properties:
list
Using the latest nuget of Automapper (6.2.2) and .Net 4.6.2 Not that it is platform or version related.
edit as said here, https://stackoverflow.com/a/5591125/169714 automapper should do list and array stuff automatically. But I keep getting unmapped properties. Even when I have the ForMember
method.
Upvotes: 1
Views: 1766
Reputation: 7668
I tried this code (with Automapper 6.2.2) and it's working on my side:
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => cfg.CreateMap<Origin, Destination>().ForMember(e => e.list, opts => opts.MapFrom(s => s.array.ToList())));
Mapper.AssertConfigurationIsValid();
Origin o = new Origin {array = new[] {"one", "two", "three"}};
Destination d = Mapper.Map<Destination>(o);
Console.WriteLine(string.Join(", ", d.list));
}
}
Upvotes: 2
Reputation: 575
Is your list containing any items? Because I can't see if you assign a value to it, from my view your list is empty...
In this thread, one of the comments was: "Now that 5.0.1 had come out I had to change this to CreateMap(MemberList.None) otherwise I would get the error".
Hope I helped!
Upvotes: 0