AutoGenMapperGenerator 2026.2.26.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package AutoGenMapperGenerator --version 2026.2.26.1
NuGet\Install-Package AutoGenMapperGenerator -Version 2026.2.26.1
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="AutoGenMapperGenerator" Version="2026.2.26.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoGenMapperGenerator" Version="2026.2.26.1" />
<PackageReference Include="AutoGenMapperGenerator" />
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 AutoGenMapperGenerator --version 2026.2.26.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoGenMapperGenerator, 2026.2.26.1"
#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 AutoGenMapperGenerator@2026.2.26.1
#: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=AutoGenMapperGenerator&version=2026.2.26.1
#tool nuget:?package=AutoGenMapperGenerator&version=2026.2.26.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
利用源生成器,在编译阶段生成映射代码,减少运行时反射
这里有一个MappingTestModel类和MappingTestModelDto类,实现对象自身的拷贝,或者MappingTestModel映射MappingTestModelDto
GenMapperAttribute标注了类型需要生成映射方法,同时实现IAutoMap接口(由生成器实现接口, 类型转换时可以用obj is IAutoMap map检查)
构造函数可选参数为目标类型,默认是自身
MapBetweenAttribute用于自定义转换动作,支持定义在类上或者属性上或者方法上,两者的有一下区别
- 定义在类上时,必须使用3个参数的构造函数,并且支持单对多,多对单的映射
- 定义在属性上,必须使用2个参数的构造函数,并且只支持单对单映射
[GenMapper(typeof(MappingTestModelDto))]
[MapBetween(typeof(MappingTestModelDto), ["Id", "Name", "Level"], "Label", By = nameof(FormatLabel))]
public partial class MappingTestModel
{
public int Id { get; set; }
[MapBetween(typeof(MappingTestModelDto), nameof(MappingTestModelDto.DisplayName))]
public string Name { get; set; }
public string Level { get; set; }
public DateTime Deadline { get; set; }
[MapBetween(typeof(MappingTestModelDto), nameof(Last),By = nameof(DTL))]
public DateTime Last { get; set; }
private static long DTL(DateTime dt)
{
return dt.Ticks;
}
private static DateTime DTL(long tick)
{
return new DateTime(tick);
}
private static string FormatLabel(int id, string name, string level)
{
return $"{id + 1}-{name}-{level}";
}
//可选反向映射方法
private static (int, string, string) FormatLabel(string label)
{
var values = label.Split('-');
return (int.Parse(values[0]), values[1], values[2]);
}
}
public class MappingTestModelDto
{
public string Id { get; set; }
public string DisplayName { get; set; }
public int Level { get; set; }
public string Deadline { get; set; }
public long Last { get; set; }
public string Label { get; set; }
}
对于MappingTestModel类型,生成器将生成如下代码
using AutoGenMapperGenerator;
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace TestProject1.Models
{
[global::System.CodeDom.Compiler.GeneratedCode("AutoGenMapperGenerator.AutoMapperGenerator", "0.0.7.0")]
/// <inheritdoc/>
partial class MappingTestModel : AutoGenMapperGenerator.IAutoMap
{
[global::System.CodeDom.Compiler.GeneratedCode("AutoGenMapperGenerator.AutoMapperGenerator", "0.0.7.0")]
public TestProject1.Models.MappingTestModelDto MapToMappingTestModelDto()
{
var _result_gen = new TestProject1.Models.MappingTestModelDto();
_result_gen.Label = TestProject1.Models.MappingTestModel.FormatLabel(this.Id, this.Name, this.Level);
_result_gen.Id = this.Id.ToString();
_result_gen.DisplayName = this.Name;
if (int.TryParse(this.Level.ToString(), out var _Level_out_gen))
{
_result_gen.Level = _Level_out_gen;
}
_result_gen.Deadline = this.Deadline.ToString();
_result_gen.Last = TestProject1.Models.MappingTestModel.DTL(this.Last);
return _result_gen;
}
[global::System.CodeDom.Compiler.GeneratedCode("AutoGenMapperGenerator.AutoMapperGenerator", "0.0.7.0")]
public void MapFromMappingTestModelDto(TestProject1.Models.MappingTestModelDto _target_gen)
{
var _Label_arr_gen = TestProject1.Models.MappingTestModel.FormatLabel(_target_gen.Label);
this.Id = _Label_arr_gen.Item1;
this.Name = _Label_arr_gen.Item2;
this.Level = _Label_arr_gen.Item3;
if (int.TryParse(_target_gen.Id, out var _Id_out_gen))
{
this.Id = _Id_out_gen;
}
this.Name = _target_gen.DisplayName;
this.Level = _target_gen.Level.ToString();
if (System.DateTime.TryParse(_target_gen.Deadline, out var _Deadline_out_gen))
{
this.Deadline = _Deadline_out_gen;
}
this.Last = TestProject1.Models.MappingTestModel.DTL(_target_gen.Last);
}
[global::System.CodeDom.Compiler.GeneratedCode("AutoGenMapperGenerator.AutoMapperGenerator", "0.0.7.0")]
public object? MapTo(string? target = null)
{
return MapToMappingTestModelDto();
}
[global::System.CodeDom.Compiler.GeneratedCode("AutoGenMapperGenerator.AutoMapperGenerator", "0.0.7.0")]
public void MapFrom(object? value)
{
if (value is TestProject1.Models.MappingTestModelDto source)
{
MapFromMappingTestModelDto(source);
}
}
}
}
因此,对于一个已有的MappingTestModel实例,你可以用如下方法获取MappingTestModelDto实例
var p = new MappingTestModel();
var pdto = p.MapToMappingTestModelDto();
// 或者使用IAutoMap接口
var dto2 = p.MapTo<MappingTestModelDto>(nameof(MappingTestModelDto))
总结
源生成器号称编译阶段的反射,性能上确定很有优势。但是目前配置的话确实不方便,需要在字段上进行Attribute标注,字段匹配规则也还没设置
感兴趣的可以看看源码生成器源码地址
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
-
net6.0
-
net8.0
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 |
|---|---|---|
| 2026.5.25.1 | 93 | 5/25/2026 |
| 2026.5.9.1 | 93 | 5/15/2026 |
| 2026.4.21.1 | 103 | 4/21/2026 |
| 2026.3.26.1 | 111 | 3/26/2026 |
| 2026.2.26.1 | 115 | 2/26/2026 |
| 2026.2.25.1 | 110 | 2/25/2026 |
| 0.1.0 | 116 | 2/5/2026 |
| 0.1.0-pre | 117 | 1/20/2026 |
| 0.0.9 | 243 | 4/30/2025 |
| 0.0.8 | 168 | 4/25/2025 |
| 0.0.7 | 223 | 10/25/2024 |
| 0.0.6 | 185 | 10/9/2024 |
| 0.0.5 | 188 | 9/5/2024 |
| 0.0.4 | 176 | 8/29/2024 |
| 0.0.3 | 210 | 8/22/2024 |
| 0.0.2 | 206 | 8/22/2024 |
| 0.0.1 | 202 | 8/22/2024 |