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