欢迎使用CSDN-markdown编辑器

来源:互联网 发布:域名注册哪家便宜 编辑:程序博客网 时间:2024/06/10 03:47

.Net Core 使用AutoMapper

  1. 安装AutoMapper
    https://www.nuget.org/packages/AutoMapper/
 <PackageReference Include="AutoMapper" Version="6.1.1" />

然后在命令行中输入 dotnet restore安装

  1. 在项目中新增一个AutoMapperHelper.cs文件

    using System.Collections;using System.Collections.Generic;using System.Data;using AutoMapper;namespace AwesomeWalle.AutoMap{public static class AutoMapperHelper{    public static T MapTo<T>(this object obj)    {        if (obj == null) return default(T);        Mapper.Initialize(p => p.CreateMap(obj.GetType(), typeof(T)));        return Mapper.Map<T>(obj);    }}}
  2. 在需要使用的地方直接引用
    WalleGroup wgroup=AutoMapperHelper.MapTo<WalleGroup>(group);

原创粉丝点击