Skip to main content

Conversions

Mapperly implements several types of automatic conversions (in order of priority):

NameDescriptionConditions
Direct assignmentDirectly assigns the source object to the targetSource type is assignable to the target type and UseDeepCloning is false
DictionaryMaps a source dictionary to an enumerable targetSource type is an IDictionary<,> or an IReadOnlyDictionary<,>
EnumerableMaps an enumerable source to an enumerable targetSource type is an IEnumerable<>
Implicit castImplicit cast operatorAn implicit cast operator is defined to cast from the source type to the target type
Parse methodUses a static Parse method on the source typeSource type is a string and target has a static method with the following signature: TTarget Parse(string).
ConstructorUses a constructor on the target type with the source as single parameterTarget type has a visible constructor with a single parameter of the source type.
String to enumMaps a string to an enum member nameSource type is a string and the target type is an enum
Enum to stringMaps an enum member name to a stringSource type is an enum and the target type is a string
Enum to enumMaps an enum to another enum either by value or by member nameSource and target types are enums
DateTime to DateOnlyMaps a DateTime to a DateOnlySource type is a DateTime and target type is a DateOnly
DateTime to TimeOnlyMaps a DateTime to a TimeOnlySource type is a DateTime and target type is a TimeOnly
Explicit castExplicit cast operatorAn explicit cast operator is defined to cast from the source type to the target type
ToStringToString method of an objectTarget type is a string
New instanceCreate a new instance of the target type and map all propertiesThe target type has a visible constructor or an object factory exists for the target type

Disable all automatic conversions

To disable all conversions supported by Mapperly set EnabledConversions to None:

[Mapper(EnabledConversions = MappingConversionType.None)]
public partial class CarMapper
{
...
}

Disable specific automatic conversions

To disable a specific conversion type, set EnabledConversions to All excluding the conversion type to disable:

// this disables conversions using the ToString() method:
[Mapper(EnabledConversions = MappingConversionType.All & ~MappingConversionType.ToStringMethod)]
public partial class CarMapper
{
...
}