AutoMapper inconsistently automatically resolves the & # 8594; Datetime

I expect AutoMapper (3.3.0) to not automatically allow string conversions -> DateTime, even if the string is in a well-understood format. Lack of inclusion of a default string -> DateTime converter is noted (albeit four years ago) in a comment by library author Jimmy Bogard in response to this StackOverflow answer: https://stackoverflow.com/a/166778/

However, I have a .NET Fiddle, which seems to suggest that AutoMapper can handle this default mapping: https://dotnetfiddle.net/dDtUGx

In this example, the Zing property is mapped from string in Foo to a DateTime string in a string without specifying a custom mapping or resolver.

However, when this code works in my test solution modules (using the same version of AutoMapper), it throws the exception that I expect:

 AutoMapper.AutoMapperMappingExceptionMissing type map configuration or unsupported mapping. Mapping types: String -> DateTime System.String -> System.DateTime Destination path: Bar.Zing Source value: Friday, December 26, 2014 

What causes this inconsistent behavior?

For completeness, the code inside the .NET script is reproduced here:

 using System; using AutoMapper; public class Program { public static void Main() { var foo = new Foo(); foo.Zing = DateTime.Now.ToLongDateString(); Mapper.CreateMap<Foo, Bar>(); var bar = Mapper.Map(foo, new Bar()); Console.WriteLine(bar.Zing); } public class Foo { public string Zing { get; set; } } public class Bar { public DateTime Zing { get; set; } } } 
+5
source share
1 answer

I believe that since Automapper v2.0 the conversion of string to DateTime handled by an internal IObjectMapper called TypeConverterMapper , but looking at the source, this seems to apply to the platform: only some of the platforms (.Net full, SL5, WinRT) provide this cartographer. The "portable" .Net assembly does not have this particular platform adapter.

If your unit tests reference the Automapper 3.3.0 portable assembly, you get an error. If they reference the complete assembly of .Net 4.0, the mapping will succeed.

+1
source

Source: https://habr.com/ru/post/1209930/


All Articles