Search code examples
c#automapper

Validate AutoMapper for type mismatches only


When I use automapper, I purposely don't map a number of properties that are identical. That's one of the main reasons for me to use automapper. Outside of that, I can build a projection or class converter myself with about the same amount of code and less reflection (i.e., more performant).

The only issue I have is that we occasionally have some type mismatches. Is it possible to use automapper's configuration validation to confirm that the types are interchangeable? I know that for some types, like string to int, that can't be validated 100% because someone could use a string that isn't convertible to an integer. But some types like timespan to datetime are not convertible without an explicit map. I would like to call those out. The existing AssertConfigurationIsValid() method fails to satisfy my needs because of the strict combination of explicit mapping/ignoring needed for it to only ever complain about type mismatches instead.


Solution

  • This is not supported by the library. One possible solution is to write our own validation process that only checks the source and destination type with some variation of casts and try/catching Convert.ChangeType().