zoukankan      html  css  js  c++  java
  • map映射

    using System;
    using AutoMapper;
    using System.Collections;
    using System.Collections.Generic;

    namespace Infrastructure
    {
    public static class AutoMapperExt
    {
    /// <summary>
    /// 类型映射
    /// </summary>
    public static T MapTo<T>(this object obj)
    {
    if (obj == null) return default(T);

    var config = new MapperConfiguration(cfg=>cfg.CreateMap(obj.GetType(),typeof(T)));
    var mapper = config.CreateMapper();
    return mapper.Map<T>(obj);
    }

    /// <summary>
    /// 集合列表类型映射
    /// </summary>
    public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
    {
    Type sourceType = source.GetType().GetGenericArguments()[0]; //获取枚举的成员类型
    var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
    var mapper = config.CreateMapper();

    return mapper.Map<List<TDestination>>(source);
    }

    /// <summary>
    /// 集合列表类型映射
    /// </summary>
    public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
    {
    var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
    var mapper = config.CreateMapper();

    return mapper.Map<List<TDestination>>(source);
    }

    /// <summary>
    /// 类型映射
    /// </summary>
    public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
    where TSource : class
    where TDestination : class
    {
    if (source == null) return destination;

    var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
    var mapper = config.CreateMapper();
    return mapper.Map<TDestination>(source);
    }

    }
    }

  • 相关阅读:
    HandlerExceptionResolver 实现全局异常捕获
    Cocoapods安装过程
    mysql 安装
    崩溃日志的字段简单说明
    简述static关键字、void与void *(void指针)、函数指针
    在python后台如何将客户端提交的form表单数据提取出来?
    iOS如何提高页面流畅度
    屏幕显示机制
    计算机网络体系小知识
    Protobuf java版本安装步骤
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/10998239.html
Copyright © 2011-2022 走看看