ZeroReflection.MapperGenerator
1.0.4
dotnet add package ZeroReflection.MapperGenerator --version 1.0.4
NuGet\Install-Package ZeroReflection.MapperGenerator -Version 1.0.4
<PackageReference Include="ZeroReflection.MapperGenerator" Version="1.0.4" />
<PackageVersion Include="ZeroReflection.MapperGenerator" Version="1.0.4" />
<PackageReference Include="ZeroReflection.MapperGenerator" />
paket add ZeroReflection.MapperGenerator --version 1.0.4
#r "nuget: ZeroReflection.MapperGenerator, 1.0.4"
#:package ZeroReflection.MapperGenerator@1.0.4
#addin nuget:?package=ZeroReflection.MapperGenerator&version=1.0.4
#tool nuget:?package=ZeroReflection.MapperGenerator&version=1.0.4
ZeroReflection.MapperGenerator
This package contains the source generator for ZeroReflection.Mapper. It analyzes MapperProfile classes at compile time and generates efficient, reflection-free mapping code using Roslyn code analysis.
Installation
This package is typically referenced automatically when you install ZeroReflection.Mapper. You don't need to install it separately unless you're building custom tooling.
For normal usage, simply install the main package:
dotnet add package ZeroReflection.Mapper
The generator will automatically run during compilation and generate mapping code based on your MapperProfile configurations.
How It Works
The source generator:
- Scans for MapperProfile classes - Analyzes all classes that inherit from
MapperProfilein your project - Parses mapping configurations - Extracts mapping pairs, custom mappings, property mappings, and ignored properties from your
Configure()method - Generates mapping code - Creates static extension methods and helper classes for each mapping pair
- Builds a dispatcher - Generates a type-safe dispatcher for runtime type resolution (switch-based or if/else)
- Creates DI extensions - Generates service registration methods for dependency injection
What It Generates
For each MapperProfile in your project, the generator creates:
1. Extension Methods for Object Mapping
public static PersonEntity MapToPersonEntity(this PersonModel source)
public static List<PersonEntity> MapListToPersonEntity(List<PersonModel> source)
public static PersonEntity[] MapArrayToPersonEntity(PersonModel[] source)
2. Generated Mapping Dispatcher
A high-performance dispatcher class that routes mapping requests based on source/destination types:
- Switch mode (default): Uses dictionary lookup + switch statement for O(1) dispatch
- If/else mode: Sequential type comparisons for smaller code size
3. Service Registration Extension
services.RegisterZeroReflectionMapping();
Automatically registers IMapper and IGeneratedMappingDispatcher with your DI container.
4. Static Mapping Classes
For each mapping pair (e.g., PersonModel → PersonEntity), generates a class like:
public static class MapPersonModelToPersonEntity
{
public static PersonEntity MapToPersonEntity(this PersonModel source) { ... }
public static List<PersonEntity> MapListToPersonEntity(List<PersonModel> source) { ... }
public static PersonEntity[] MapArrayToPersonEntity(PersonModel[] source) { ... }
}
Configuration Options
The generator respects these configuration properties set in your MapperProfile:
UseSwitchDispatcher(default:true) - Use switch-based dispatcher vs if/else chainsThrowIfPropertyMissing(default:false) - Generate build-time errors for unmapped destination propertiesEnableProjectionFunctions- Disabled/ignored for AOT safety
Custom Mapping Support
The generator handles:
- Property-level custom mappings - Via
ForMember()configuration - Full custom mappings - Via
WithCustomMapping()with static methods - Property ignoring - Via
Ignore()or[IgnoreMap]attribute - Property renaming - Via
[MapTo("DestinationProperty")]attribute
AOT Compatibility
The generator produces AOT-friendly code:
- No runtime reflection
- No
Expression.Compile() - All mapping logic generated at compile time
- Only static custom mapping methods are supported
Example Profile
public class MyMapperProfile : MapperProfile
{
public override void Configure(MapperConfiguration config)
{
config.UseSwitchDispatcher = true;
config.ThrowIfPropertyMissing = false;
config.CreateMap<PersonModel, PersonEntity>()
.ForMember(dest => dest.Id, src => Guid.NewGuid())
.Ignore(dest => dest.InternalField);
config.CreateMap<ProductEntity, ProductModel>()
.WithCustomMapping(StaticMappers.MapProduct);
}
}
Requirements
- .NET Standard 2.0 or higher
- Microsoft.CodeAnalysis.CSharp 4.0.1+
- Roslyn source generator support
License
MIT
Repository
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.