PhantoMapper
PhantoMapper is a lightweight .NET mapper implemented as a source generator and distributed as a NuGet package. It generates efficient, allocation-friendly mapping code at compile time and minimal overhead at runtime.
This project is still in early stages of development and not public yet!
Demonstraited APIs and Project name might change in the design and development process!
Key ideas
- Use attributes to declare mappings: apply
MapFrom and MapTo on partial classes, records, or structs to map from or to other types.
- Source-generation: mapping code is generated at compile time for fast runtime execution and no reflection-based overhead.
- Attribute-driven configuration: control property mapping, value conversions, enum mapping, and ignore rules via attributes.
- Generated targets: single object mapping plus collections —
IEnumerable<T>, arrays, and List<T> are supported out of the box.
- Convenience API: the generator adds static mapping methods and operators into the same partial class/record/struct for ergonomic usage (e.g.,
From, To, and explicit/implicit operators).
Features
- Map between classes, records, and structs using
MapFrom / MapTo.
- Configure mapping per-property with attributes (rename, transform, ignore, default).
- Support for value conversions and enum mapping strategies.
- Generated mapping methods for single instances and collections (IEnumerable, Array, List).
- Static methods and operators injected into the partial type for direct mapping calls.
- Zero-reflection runtime behavior and minimal allocations.
- Supports all .NET versions (built using .Net standard 2.0).
Example
// Source type with mapping attribute
// mapping functions will be generated in UserDto
[MapFrom(typeof(User))]
[MapTo(typeof(User))]
public partial record UserDto(int Id, string FullName);
// Target type remains untouched
public class User
{
public int Id { get; set; }
public string FullName { get; set; }
}
After compile time generation you can use generated helpers like:
var user = (User) dto; // single object
var users = UserDto.ToUsers(dtos.ToList()); // collection mapping
Notes
- Designed for performance and clarity: mapping rules are explicit and compiled into the assembly.
- Current collection support: IEnumerable, Array, and List. More collection types may be added later.
- Works with partial classes, records, and structs — annotate the partial declaration to enable generation.