schangxiang@126.com
2025-09-19 fc752b66a7976188c4edd5e3fb7ca6bb2822e441
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
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace wcftest
{
    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;
        }
 
    }
}