schangxiang@126.com
2024-12-03 761aaf597639c55c6f36b03f3a89ba2d49a1d3f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using iWareModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareCommon.Utils
{
    public class ClassHelper
    {
        /// <summary>
        /// 实体互转
        /// </summary>
        /// <typeparam name="T">新转换的实体</typeparam>
        /// <typeparam name="S">要转换的实体</typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static T RotationMapping<T, S>(S s)
        {
            T target = Activator.CreateInstance<T>();
            var originalObj = s.GetType();
            var targetObj = typeof(T);
            foreach (PropertyInfo original in originalObj.GetProperties())
            {
                foreach (PropertyInfo t in targetObj.GetProperties())
                {
                    if (t.Name == original.Name)
                    {
                        t.SetValue(target, original.GetValue(s, null), null);
                    }
                }
            }
            return target;
        }
 
        /// <summary>
        /// 通过实体类对象获取实体类的属性对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static List<PropertieModel> GetPropertieModels<T>(T t)
        {
            List<PropertieModel> pmList = new List<PropertieModel>();
            var pros = typeof(T).GetProperties();
            string value = "";
            PropertieModel column = null;
            object objValue = null;
            foreach (var pro in pros)
            {
                column = new PropertieModel();
                //值
                objValue = pro.GetValue(t, null);
                value = objValue == null ? "null" : objValue.ToString();
                column.DataValue = value;
                //属性名
                column.PropertyName = pro.Name;
                object[] objs = pro.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objs.Length > 0)
                {
                    column.DescriptionName = ((DescriptionAttribute)objs[0]).Description;
                }
                pmList.Add(column);
            }
            return pmList;
        }
 
        
    }
}