AutoGenMapperGenerator 0.0.9
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package AutoGenMapperGenerator --version 0.0.9
NuGet\Install-Package AutoGenMapperGenerator -Version 0.0.9
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="0.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoGenMapperGenerator" Version="0.0.9" />
<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 0.0.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoGenMapperGenerator, 0.0.9"
#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@0.0.9
#: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=0.0.9
#tool nuget:?package=AutoGenMapperGenerator&version=0.0.9
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 | net5.0 was computed. net5.0-windows was computed. 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 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.