首页 > 其他分享 >AutoMapper MapHelper

AutoMapper MapHelper

时间:2022-12-01 09:56:27浏览次数:43  
标签:obj System MapHelper propertiesDic ignorDesc TOut result AutoMapper


    public static class MapperHelper
    {
        /// <summary>
        /// 将数据映射到指定的对象中
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="obj"></param>
        /// <param name="outObj"></param>
        /// <param name="ignorDesc"></param>
        /// <returns></returns>
        public static TOut AutoMap<TIn, TOut>(TIn obj, TOut outObj, bool ignorDesc = true) where TOut : new()
        {
            return AutoMap(obj, ignorDesc, outObj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="obj"></param>
        /// <param name="ignorDesc">忽略description描述信息</param>
        /// <returns></returns>
        public static TOut AutoMap<TIn, TOut>(TIn obj, bool ignorDesc = true) where TOut : new()
        {
            TOut result = new TOut();
            return AutoMap(obj, ignorDesc, result);
        }

        private static TOut AutoMap<TIn, TOut>(TIn obj, bool ignorDesc, TOut result) where TOut : new()
        {
            System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();

            //存储源对象属性
            Dictionary<string, PropertyInfo> propertiesDic = new Dictionary<string, PropertyInfo>();
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                propertiesDic.Add(item.Name, item);
            }

            System.Reflection.PropertyInfo[] resultProperties = result.GetType().GetProperties();

            foreach (System.Reflection.PropertyInfo j in resultProperties)
            {
                try
                {
                    ////自定义属性处理别名
                    DescriptionAttribute desc = (DescriptionAttribute)j.GetCustomAttributes(false).FirstOrDefault(f => f.GetType() == typeof(DescriptionAttribute));
                    if (desc != null && !ignorDesc)
                    {
                        string desName = desc.Description;
                        if (propertiesDic.ContainsKey(desName))
                        {
                            j.SetValue(result, propertiesDic[desName].GetValue(obj));
                            continue;
                        }
                    }
                    else
                    {
                        if (propertiesDic.ContainsKey(j.Name))
                        {
                            j.SetValue(result, propertiesDic[j.Name].GetValue(obj));
                        }
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        j.SetValue(result, Activator.CreateInstance(j.PropertyType));
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("转换前后类型不一致");
                    }
                }
            }
            return result;
        }

        public static IList<TOut> AutoMap<TIn, TOut>(this List<TIn> list, bool ignorDesc = true) where TOut : new()
        {
            List<TOut> result = new List<TOut>();
            foreach (TIn item in list)
            {
                try
                {
                    result.Add(AutoMap<TIn, TOut>(item, ignorDesc));
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            return result;
        }


        public static object AutoMapByType(object obj, Type outType, bool ignorDesc = false)
        {
            object result = Activator.CreateInstance(outType);

            System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();

            //存储源对象属性
            Dictionary<string, PropertyInfo> propertiesDic = new Dictionary<string, PropertyInfo>();
            foreach (System.Reflection.PropertyInfo item in properties)
            {
                propertiesDic.Add(item.Name, item);
            }

            System.Reflection.PropertyInfo[] resultProperties = outType.GetProperties();

            foreach (System.Reflection.PropertyInfo j in resultProperties)
            {
                try
                {
                    ////自定义属性处理别名
                    DescriptionAttribute desc = (DescriptionAttribute)j.GetCustomAttributes(false).FirstOrDefault(f => f.GetType() == typeof(DescriptionAttribute));
                    if (desc != null && !ignorDesc)
                    {
                        string desName = desc.Description;
                        if (propertiesDic.ContainsKey(desName))
                        {
                            j.SetValue(result, propertiesDic[desName].GetValue(obj));
                            continue;
                        }
                    }
                    else
                    {
                        if (propertiesDic.ContainsKey(j.Name))
                        {
                            j.SetValue(result, propertiesDic[j.Name].GetValue(obj));
                        }
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        j.SetValue(result, Activator.CreateInstance(j.PropertyType));
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("转换前后类型不一致");
                    }
                }
            }
            return result;
        }
    }

标签:obj,System,MapHelper,propertiesDic,ignorDesc,TOut,result,AutoMapper
From: https://www.cnblogs.com/ives/p/16940521.html

相关文章

  • .NetCore【工作应用】AutoMapper
    AutoMapperOOM(Object-Object-Mapping)组件为了实现实体间的相互转换,从而避免我们每次采用手工的方式进行转换。使用安装nuget包install-packageAutoMapperinstall-......
  • 使用 AotoFac 注册AutoMapper
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;usingAutofac;us......
  • 通过对象属性名称从AutoMapper获取目标对象属性
    一、什么是AutoMapperAutoMapper的作用是把一个对象转化为另一个对象,避免每次都去转化。使用DTO实现表现层与领域Model的解耦,用AutoMapper来实现DTO与领域Model的相互转......
  • .net Core MVC 2.0项目中如果引入AutoMapper
    第一步骤:Nuget中引入AutoMapper依赖注入包 第二步:创建一个类并继承Profile基类,并创建映射,如果需要互相映射需要调用ReverseMap()方法,如果需求忽略某些字段不进行映射,......
  • AutoMapper在.Net Core WebApi中使用
    在.NetCoreWebApi里使用AutoMapper1.安装AutoMapper管理包 注意:service层中安装WebApi层也需要安装因为Webpi层有时候也需要用到Dto 2.startup在Configure......
  • Add AutoMapper
    (注:本文示例使用的是.NETCore3.1)1.配置(Configuration)Reference:https://docs.automapper.org/en/latest/Configuration.html多种方法配置,这里推荐使用配置文件......
  • NET添加数据映射的两种方式(手动Controller和自动AutoMapper)
    手动添加映射在Controllers目录下找到所需要添加映射的xxxcontroller.cs文件,然后手动添加数据的映射:usingSystem;usingSystem.Linq;usingAutoMapper;usingMic......
  • .NET6 使用 AutoMapper (解析)
    一、Net6环境下的.netcore项目里如何使用AutoMapper实现依赖注入。注:AutoMapper是一个对象-对象映射器,可以将一个对象映射到另一个对象。第一步,在Nuget引入......
  • .net6 使用 AutoMapper
    引用AutoMapper.Extensions.Microsoft.Dependencylnjection包组织映射配置的一个好方法是使用配置文件。创建继承自Profile并将配置放入构造函数的类publicclassR......
  • EFCore join table and AutoMapper
    EFCorejointableandAutoMapperQuestionIwanttoqueryallusersfrommyASP.netIdentityUserstableandmapthemtoasimpleDTOlikethis:publicclass......