using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace DataCapture_MA.Util { public class ClassHelper { /// /// 实体互转 /// /// 新转换的实体 /// 要转换的实体 /// /// public static T RotationMapping(S s) { T target = Activator.CreateInstance(); 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; } /// /// 通过实体类对象获取实体类的属性对象集合 /// /// /// /// public static List GetPropertieModels(T t) { List pmList = new List(); 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; } } }