ZeroReflection.MapperGenerator 1.0.4

dotnet add package ZeroReflection.MapperGenerator --version 1.0.4
                    
NuGet\Install-Package ZeroReflection.MapperGenerator -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZeroReflection.MapperGenerator" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZeroReflection.MapperGenerator" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="ZeroReflection.MapperGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZeroReflection.MapperGenerator --version 1.0.4
                    
#r "nuget: ZeroReflection.MapperGenerator, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZeroReflection.MapperGenerator@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZeroReflection.MapperGenerator&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=ZeroReflection.MapperGenerator&version=1.0.4
                    
Install as a Cake Tool

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:

  1. Scans for MapperProfile classes - Analyzes all classes that inherit from MapperProfile in your project
  2. Parses mapping configurations - Extracts mapping pairs, custom mappings, property mappings, and ignored properties from your Configure() method
  3. Generates mapping code - Creates static extension methods and helper classes for each mapping pair
  4. Builds a dispatcher - Generates a type-safe dispatcher for runtime type resolution (switch-based or if/else)
  5. 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., PersonModelPersonEntity), 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 chains
  • ThrowIfPropertyMissing (default: false) - Generate build-time errors for unmapped destination properties
  • EnableProjectionFunctions - 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

GitHub

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 133 11/28/2025
1.0.3 214 11/25/2025