1

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.

2
  • 1
    Why don't you configure the member mapping in new MapperConfiguration instead of calling Mapper.Initialize. Do you expect the initialize to have any effect on your newly created configuration? Commented Jan 4, 2018 at 9:09
  • omg, i was asleep. Thanks! Commented Jan 4, 2018 at 9:12

2 Answers 2

2

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.