Conversions
Mapperly implements several types of automatic conversions (in order of priority):
| Name | Description | Conditions |
|---|---|---|
| Direct assignment | Directly assigns the source object to the target | Source type is assignable to the target type and UseDeepCloning is false |
| Queryable | Projects the source queryable to the target queryable | Source and target types are IQueryable<> |
| Dictionary | Maps a source dictionary to an enumerable target | Source type is an IDictionary<,> or an IReadOnlyDictionary<,> |
| Enumerable | Maps an enumerable source to an enumerable target | Source type is an IEnumerable<> |
| Span | Maps a Span<>, ReadOnlySpan<> to or from Span<>, ReadOnlySpan<> or enumerable | Source or target type is a Span<>, ReadOnlySpan<> |
| Tuple | Create a new instance of a ValueTuple or tuple expression i.e. (10, 12) | Target type is a ValueTuple<> or tuple expression |
| Memory | Maps a Memory<>, ReadOnlyMemory<> to or from Memory<>, ReadOnlyMemory<>, Span<>, ReadOnlySpan<> or enumerable | Source or target type is a Memory<> or ReadOnlyMemory<> |
| Implicit cast | Implicit cast operator | An implicit cast operator is defined to cast from the source type to the target type |
| Parse method | Uses a static Parse method on the target type | Source type is a string and target has a static method with the following signature: TTarget Parse(string). |
| Constructor | Uses a constructor on the target type with the source as single parameter | Target type has a visible constructor with a single parameter of the source type. |
| String to enum | Maps a string to an enum member name | Source type is a string and the target type is an enum |
| Enum to string | Maps an enum member name to a string | Source type is an enum and the target type is a string |
| Enum to enum | Maps an enum to another enum either by value or by member name | Source and target types are enums |
| Explicit cast | Explicit cast operator | An explicit cast operator is defined to cast from the source type to the target type |
| ToString | ToString method of an object | Target type is a string |
| ToTarget | ToTTarget method of an object, excluded ToString | Source object contains method with signature TTarget ToTTarget() |
| DateTime to DateOnly | Maps a DateTime to a DateOnly | Source type is a DateTime and target type is a DateOnly |
| DateTime to TimeOnly | Maps a DateTime to a TimeOnly | Source type is a DateTime and target type is a TimeOnly |
| Static method | Convert source uses source type static ToTTarget method or target type static CreateFromTTarget, FromTSource, Create, CreateFrom, except for converting DateTime to DateOnly or TimeOnly | The source type contains a static method with signature TTarget ToTTarget() or the target type contains one of the following static methods TTarget Create(TSource), TTarget CreateFrom(TSource), TTarget CreateFromTTarget(TSource) TTarget FromTSource(TSource) |
| New instance | Create a new instance of the target type and map all properties | The target type has a visible constructor or an object factory exists for the target type |
Note: To exclude a candidate conversion method (Parse, ToTarget instance methods, or static Create/CreateFrom/From/To methods), annotate that method with [MapperIgnore].
This only works when the annotated method is in the same assembly as the mapper unless Mapperly attributes are preserved at runtime; see the "Ignore a member at definition" section in the mapper configuration docs.
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
{
...
}