C#反射 类转换类、类集合转换类集合

最近发现总会有需求,把一个实体类赋值给另一个实体类,在网上找了找,最后用反射写的动态转换类。但是只测试了DBModel的赋值和转换。不多说上代码。

        /// <summary>
        /// 类转类(T是接收的类型,A是传入数据的类型)
        /// 注意,不可以转换集合,转换集合请用SetListValueByListT方法
        /// 当前类用来转换实体类对象,只是属性和属性的赋值转换
        /// 只转换名字相同并且类型一致的
        /// </summary>
        /// <typeparam name="T">转换结果类型</typeparam>
        /// <typeparam name="A">要转换的类型</typeparam>
        /// <param name="GetValue">传入的参数</param>
        /// <returns>转换结果</returns>
        public T SetValueByT<T, A>(A GetValue)
        {
            var SetValue = System.Activator.CreateInstance<T>();
            try
            {
                var TProps = SetValue.GetType().GetProperties();
                var TPropsNames = TProps.ToDictionary(i => i.Name, i => i.GetType());
                var TPropsTypes = TProps.Select(i => i.GetType()).ToList();

                var AProps = GetValue.GetType().GetProperties();

                List<PropertyInfo> Props = new List<PropertyInfo>();
                foreach (var item in AProps)
                {
                    if (TPropsNames.Keys.Contains(item.Name))
                    {
                        if (TPropsNames[item.Name] == item.GetType())
                        {
                            var Value = GetValue.GetType().GetProperty(item.Name).GetValue(GetValue);
                            SetValue.GetType().GetProperty(item.Name).SetValue(SetValue, Value);
                        }
                    }
                }
                return SetValue;
            }
            catch
            {
                return SetValue;
            }
        }

        /// <summary>
        /// 集合转换(T是接收的集合类型,A是传入数据的集合类型)
        /// 当前类用来转换实体类对象,只是属性和属性的赋值转换
        /// 只转换名字相同并且类型一致的
        /// </summary>
        /// <typeparam name="T">转换结果类型</typeparam>
        /// <typeparam name="A">要转换的类型</typeparam>
        /// <param name="GetListValue">传入的集合</param>
        /// <returns>转换结果</returns>
        public List<T> SetListValueByListT<T, A>(List<A> GetListValue)
        {
            List<T> SetListValue = new List<T>();
            if (GetListValue.Count() > 0)
            {
                var SetDefaultValue = System.Activator.CreateInstance<T>();
                var TProps = SetDefaultValue.GetType().GetProperties();
                var TPropsNames = TProps.ToDictionary(i => i.Name, i => i.GetType());
                var TPropsTypes = TProps.Select(i => i.GetType()).ToList();
                var AProps = GetListValue.First().GetType().GetProperties();

                Dictionary<string, List<object>> list = new Dictionary<string, List<object>>();
                foreach (var item in AProps)
                {
                    if (TPropsNames.Keys.Contains(item.Name))
                    {
                        if (TPropsNames[item.Name] == item.GetType())
                        {
                            List<object> getPropList = GetListValue.Select(i => i.GetType().GetProperty(item.Name).GetValue(i)).ToList();
                            list.Add(item.Name, getPropList);
                        }
                    }
                }
                if (list.Keys.Count > 0)
                {
                    var Count = list.ElementAt(0).Value.Count;
                    for (int i = 0; i < Count; i++)
                    {
                        var NewValue = System.Activator.CreateInstance<T>();
                        foreach (var item in list)
                        {
                            NewValue.GetType().GetProperty(item.Key).SetValue(NewValue, item.Value.ElementAt(i));
                        }
                        SetListValue.Add(NewValue);
                    }
                }
            }
            return SetListValue;
        }

测试一下是否成功

先创建两个测试类

public class Class1
    {
        public string Name { get; set; }
        public string ID { get; set; }
        //这里名称不一样
        public string Info { get; set; }
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }
    }

public class Class2
    {
        public string Name { get; set; }
        public string ID { get; set; }
        //这里名称不一样
        public string Infomation { get; set; }
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }
    }
View Code

测试一下类转类的结果

Class2 class2 = new Class2 { ID = "1213", Name = "name", Infomation = "测试", Value1 = 1, Value2 = 2, Value3 = 3 };
Class1 class1 = SetValueByT<Class1, Class2>(class2);

在测试一下集合和集合的转换,插入1万条数据

List<Class2> class2s = new List<Class2>();
for (int i = 0; i < 10000; i++)
{
    class2s.Add(new Class2 { ID = i.ToString(), Name = "name", Infomation = "测试", Value1 = i + 1, Value2 = i + 2, Value3 = 1 + 3 });
}

List<Class1> class1s = SetListValueByListT<Class1, Class2>(class2s);

成功了!

测试一下耗时一万条数据用了134毫秒

猜你喜欢

转载自www.cnblogs.com/dhdd/p/10438829.html