AutoMapUtility

实体属性自动映射,支持对象间属性值复制和List复制

安装

Install-Package AutoMapUtility -Version 1.0.2

https://github.com/leoparddne/AutoMapUtility

//定义实体,可通过Description指定映射的属性
public class T
{
public int a { get; set; }
public int b { get; set; }

}

public class T2
{
public string a { get; set; }
[Description("b")]
public int bb { get; set; }

}
//使用
var t = new T { a = 1, b = 2 };
var t2 = t.Map<T2>();
Console.WriteLine(JsonConvert.SerializeObject(t2));

var list1 = new List<T> { new T{a=1,b=2},new T { a = 3, b = 4 } };
IList<T2> list2 = list1.MapList<T, T2>();
Console.WriteLine(JsonConvert.SerializeObject(list2));\

 

 

后续优化

 

    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;
        }
    }

 

posted @ 2019-11-19 11:15  Hey,Coder!  阅读(147)  评论(0编辑  收藏  举报