Inkslab.Map 1.2.25

dotnet add package Inkslab.Map --version 1.2.25
                    
NuGet\Install-Package Inkslab.Map -Version 1.2.25
                    
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="Inkslab.Map" Version="1.2.25" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Inkslab.Map" Version="1.2.25" />
                    
Directory.Packages.props
<PackageReference Include="Inkslab.Map" />
                    
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 Inkslab.Map --version 1.2.25
                    
#r "nuget: Inkslab.Map, 1.2.25"
                    
#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 Inkslab.Map@1.2.25
                    
#: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=Inkslab.Map&version=1.2.25
                    
Install as a Cake Addin
#tool nuget:?package=Inkslab.Map&version=1.2.25
                    
Install as a Cake Tool

Inkslab

Inkslab.Map 是什么?

Inkslab.MapIMapper 的默认实现——一个基于表达式树的约定优先对象映射器,适用于 DTO / 实体 / VM 之间的双向转换。

  • 属性名忽略大小写自动匹配。
  • 支持集合 / 数组 / Dictionary 映射。
  • 支持构造函数映射New)。
  • 支持属性级自定义MapFromConstantIgnore
  • 支持继承与包含映射IncludeProfile
  • 基于表达式树编译,安全映射非空类型间关系,无须处理 null/Nullable 分支。

AutoMapper 定位不同:本框架优先"安全对象深拷贝 + 自动匹配",AutoMapper 优先"值转换 + 显式映射"。需要类似 AutoMapper 的表现,可禁用深拷贝配置。


安装

dotnet add package Inkslab.Map

安装后由 MStartup 自动注册 IMapper 默认实现。


快速入门

1. 自动映射

FooDto foo = Mapper.Map<FooDto>(fooEntity);
BarDto bar = Mapper.Map<BarDto>(barEntity);

2. 集合 / 数组 / 字典

var arr  = Mapper.Map<UserDto[]>(userArr);
var list = Mapper.Map<List<UserDto>>(users);
var dict = Mapper.Map<Dictionary<string, object>>(user);

3. 构造函数映射

目标类型具备匹配构造函数时自动选用:

public record B(int Id, string Name, DateTime CreatedAt);

var b = Mapper.Map<B>(a);

核心契约

IMapper src/Inkslab/Map/IMapper.cs

public interface IMapper
{
    T      Map<T>(object obj);
    object Map   (object obj, Type destinationType);
}

静态门面 Mapper src/Inkslab/Map/Mapper.cs

Mapper.Map<UserDto>(user);
Mapper.Map(user, typeof(UserDto));

默认实现 MapperInstance src/Inkslab.Map/MapperInstance.cs

继承 Profile,实例化后可在当前范围内配置定制规则(using 确保释放)。

using var instance = new MapperInstance();
instance.Map<From, To>()
        .Map(...);

var dto = instance.Map<To>(source);

进阶用法

1. 属性级自定义

using var instance = new MapperInstance();

instance.Map<C1, C2>()
    .Map(x => x.R1, y => y.From(z => z.P1))                    // 字段改名
    .Map(x => x.T3, y => y.From(z => z.P3.ToString()))         // 类型转换
    .Map(x => x.D4, y => y.Constant(DateTimeKind.Utc))         // 常量
    .Map(x => x.I5, y => y.Ignore());                          // 忽略属性

var dst = instance.Map<C2>(src);

2. 继承与包含

instance.Map<C2, C1>()
        .Include<C3>()                                         // 规则同时作用于 C2→C1 与 C2→C3
        .Map(x => x.P1, y => y.From(z => z.R1))
        .Map(x => x.P3, y => y.From(z => Convert.ToDateTime(z.T3)));

var c3 = instance.Map<C3>(src);

3. 构造函数自定义

instance.New<C1, C4>(x => new C4(x.P1))
        .Map(x => x.T3, y => y.From(z => z.P3.ToString()))
        .Map(x => x.D4, y => y.Constant(DateTimeKind.Utc))
        .Map(x => x.I5, y => y.Ignore());

4. 集合到集合的自定义构造

instance.Map<C1, C2>()
        .NewEnumerable<PagedList<C1>, PagedList<C2>>(
            (src, mapped) => new PagedList<C2>(mapped, src.PageIndex, src.PageSize, src.Total));

5. 使用 Profile 预定义规则

public class OrderProfile : Profile
{
    public OrderProfile()
    {
        Map<OrderEntity, OrderDto>()
            .Map(d => d.PayTime, o => o.From(s => s.PaidAt));
    }
}

// 向全局映射配置注册(必须在 Mapper.Map<T> 首次调用之前):
MapConfiguration.Instance.AddProfile(new OrderProfile());
// 或按类型注册(内部自动实例化,要求有无参或参数均有默认值的构造函数):
MapConfiguration.Instance.AddProfile(typeof(OrderProfile));

内置映射规则

默认实现内置多种常用规则(位于 src/Inkslab.Map/Maps/),按优先级匹配:

规则 场景
CloneableMap 源类型实现 ICloneable 时的克隆
ConvertMap System.Convert 覆盖的基础类型
EnumUnderlyingTypeMap 枚举 ↔ 其底层数值类型
StringToEnumMap 字符串 → 枚举
ParseStringMap 字符串 → Guid / Version / TimeSpan / DateTimeOffset
ParseStringToBooleanMap 字符串 → bool
ToStringMap 任意对象 → string
KeyValueMap KeyValuePair<,>KeyValuePair<,>
FromKeyIsStringValueIsAnyMap IDictionary<string,*> → 对象
ToKeyIsStringValueIsObjectMap 对象 → IDictionary<string, object>
EnumerableMap 集合 ↔ 集合
ConstructorMap 构造函数单参注入
DefaultMap 兜底:属性名忽略大小写匹配

自定义实现

public class MyMapper : IMapper
{
    public T      Map<T>(object obj)                        => /* ... */;
    public object Map   (object obj, Type destinationType)  => /* ... */;
}

// 启动前替换
SingletonPools.TryAdd<IMapper, MyMapper>();

性能与注意事项

  • 默认深拷贝:源引用不会被共享,避免后续修改误伤。
  • 不支持循环引用:递归关系会抛出或导致栈溢出;请显式 Ignore 自引用。
  • 复用 MapperInstance:批量映射时复用同一实例以命中表达式编译缓存。
  • 线程安全MapperInstance 的配置阶段非线程安全;应用(Map 调用)线程安全。

单元测试


说明

  • AutoMapper 的核心差异:
    • 本框架无需强制声明映射关系——未声明的属性按名称忽略大小写自动匹配。
    • 本框架支持集合到集合的自定义构造。
    • 本框架默认深拷贝,避免数据源被意外共享。
  • 基于表达式树分析 + 组合实现映射,运行时零反射开销。
Product 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 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. 
.NET Framework net461 is compatible.  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 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.
  • .NETFramework 4.6.1

  • .NETStandard 2.1

  • net6.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
1.2.25 93 6/9/2026
1.2.24 116 4/22/2026
1.2.23 122 3/27/2026
1.2.22 116 3/24/2026
1.2.20 111 3/24/2026
1.2.18 248 10/9/2025
1.2.17 243 8/15/2025
1.2.16 180 6/7/2025
1.2.15 260 5/8/2025
1.2.14 301 11/22/2024
1.2.13 720 9/10/2024
1.2.12 258 9/10/2024
1.2.11 335 8/4/2024
1.2.10 203 7/29/2024
1.2.9 209 7/29/2024
1.2.8.5 3,159 5/15/2024
1.2.8.1 1,990 4/7/2024
1.2.8 441 3/26/2024
1.2.7 324 3/1/2024
1.2.6 448 1/19/2024
Loading failed