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
QueryableProjects the source queryable to the target queryableSource and target types are IQueryable<>
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<>
SpanMaps a Span<>, ReadOnlySpan<> to or from Span<>, ReadOnlySpan<> or enumerableSource or target type is a Span<>, ReadOnlySpan<>
TupleCreate a new instance of a ValueTuple or tuple expression i.e. (10, 12)Target type is a ValueTuple<> or tuple expression
MemoryMaps a Memory<>, ReadOnlyMemory<> to or from Memory<>, ReadOnlyMemory<>, Span<>, ReadOnlySpan<> or enumerableSource or target type is a Memory<> or ReadOnlyMemory<>
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 target 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
{
...
}

Enable only specific automatic conversions

To enable only specific conversion types, set EnabledConversions the conversion type to enable:

// This disables conversions using the ToString() method, which is enabled by default:
[Mapper(EnabledConversions = MappingConversionType.Constructor | MappingConversionType.ExplicitCast)]
public partial class CarMapper
{
...
}