2

I have the following class:

public class Response{
    public string Result {get;set;}
    public ArrayList Errors {get;set;}
}

and the following mappings

cfg.CreateMap<ErrorMessage, Error>();

cfg.CreateMap<OriginalResponse, Response>()
   .ForMember(d => d.Errors, 
              opts => opts.MapFrom(s => s.ErrorMessages));

In my source, ErrorMessages is an Array of type ErrorMessage.

I would like like the Errors ArrayList in my response to be of type Error but my mapping is returning an ArrayList of type ErrorMessage (the original type).

How could I get the ArrayList to map correctly?

I can't use a regular array of type Errors because of the limitations of a 3rd party system.

Repro here: https://dotnetfiddle.net/VPRsYV

7
  • Who is teaching the use of ArrayList in .net these days? Commented Aug 26, 2019 at 5:49
  • @TheGeneral being forced to use ArrayList in a 3rd party system unfortunately Commented Aug 26, 2019 at 5:55
  • 1
    You might have to create a TypeConverter docs.automapper.org/en/stable/Custom-type-converters.html Commented Aug 26, 2019 at 5:58
  • You don't need a type converter. That works for me using the latest from NuGet. A repro would help. Make a gist that we can execute and see fail. Commented Aug 26, 2019 at 6:37
  • It works for me but the ArrayList is of the source type (ErrorMessage) and not the destination type (Error) Commented Aug 26, 2019 at 6:43

1 Answer 1

2

Try

cfg.CreateMap<ErrorMessage, Error>(); cfg.CreateMap<ErrorMessage, object>().As<Error>();

This tells AM that when you map from ErrorMessage to object (there is no element type information in ArrayList), you actually mean to Error. Check the docs.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it is now of the correct type - but the properties of the mapped type are null. See dotnetfiddle.net/8R6QTj. Would I need to specify .ForMember() for each property that is mapped?
Updated. You're missing a map.

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.