0

Given the following classes

public class FirstFoo {
    public string Bar { get; }
    public string Baz { get; }

    public FirstFoo(string bar, string baz)
    {
        Bar = bar;
        Baz = baz;
    }
}

public class SecondFoo {
    public string Bar { get; }
    public string Baz { get; }

    public SecondFoo(string bar, string baz)
    {
        Bar = bar;
        Baz = baz;
    }
}

and the following mapping definition

Mapper.CreateMap<FirstFoo, SecondFoo>();

I would have expected Mapper.Map<SecondFoo>(firstFooInstance) to Just Work(TM), but it throws an ArgumentException

System.ArgumentException: Type 'SecondFoo' does not have a default constructor

Am I doing something wrong here?

Disclaimer: We're still using AutoMapper 2.2.1. I've perused the change log to figure out if this feature was introduced in a later release, but I've only found bugfixes or other improvement when searching for "constructor" in all entries, and 2.2.1 and earlier releases don't have any details in the change log, so I can't even confirm that it should work. (And yes, I'm aware this is a very old release. I'm looking at updating to the latest release across the large enterprise solution this is part of, but it's not a priority issue. It might become one, if it solves this issue, but I won't spend time on it just in case...)

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • Have you tried using .ConstructUsing for the constructor parameters? – JsonStatham Dec 17 '15 at 09:56
  • Not entirely sure if this is available in your version though... – JsonStatham Dec 17 '15 at 09:57
  • @selectDistinct: I haven't been able to find a way to use `ConstructUsing` that doesn't require me to also explicitly map all arguments, which sort-of defeats the purpose to use AutoMapper in the first place... – Tomas Aschan Dec 17 '15 at 11:41
  • But maybe I just don't understand how to use `ConstructUsing` correctly... – Tomas Aschan Dec 17 '15 at 11:43
  • Have you had a look at this question: http://stackoverflow.com/questions/2239143/automapper-how-to-map-to-constructor-parameters-instead-of-property-setters – JsonStatham Dec 17 '15 at 11:45
  • Yes, but that just illustrates my problem: I have to specify the constructor parameters explicitly. I'm looking into [this blog post](http://www.productiverage.com/teaching-automapper-about-verbose-constructors) at the moment, to see if this can be done in a better way. – Tomas Aschan Dec 17 '15 at 11:50
  • Your code does not compile, do you have private setters for the properties? – Yacoub Massad Dec 17 '15 at 14:30
  • @YacoubMassad: It compiles in VS2015 (the syntax is from C# 6, and we're using the `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` nuget package to get access to them in .NET 4.5). – Tomas Aschan Dec 17 '15 at 14:59
  • With C# 5 (and .NET 4.5), the issue is solved by simply adding private setters to the properties. Is this an option for you? – Yacoub Massad Dec 17 '15 at 15:04
  • @YacoubMassad: I tried that, but it didn't help. – Tomas Aschan Dec 17 '15 at 16:30

1 Answers1

0

Wow, this is a little embarassing. Apparently, the type I was mapping to had a property which the type I was mapping from didn't have, meaning that there was no way to fill all the constructor arguments. I was totally thrown off by the error message about there being no default constructor (had I mapped to mutable properties, AutoMapper would have complained about an invalid mapping and given me a hint on which property was missing...).

Update: I filed an issue with AutoMapper, and this will be fixed in the next release!

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402