C# model转Dto 使用泛型即可实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace GlobalAPI
{
public class PropertyHelper
{
/// <summary>
/// 反射得到实体类的字段名称和值
/// var dict = GetProperties(model);
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="t">实例化</param>
/// <returns></returns>
public static Dictionary<object, object> GetProperties<T>(T t)
{
var ret = new Dictionary<object, object>();
if (t == null) { return null; }
PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (properties.Length <= 0) { return null; }
foreach (PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType == typeof(DateTime))
{
ret.Add(name, Convert.ToDateTime(value).ToString("yyyy/MM/dd HH:mm:ss.fff"));
}
else if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
ret.Add(name, value);
}
}
return ret;
}
/// <summary>
/// 给对象赋值的方法(不赋地址)(同一个类型)
/// </summary>
/// <param name="left">=号左边</param>
/// <param name="right">=号右边</param>
/// <summary>
/// 给对象赋值的方法(不赋地址)(不同类型)
/// </summary>
/// <param name="left">=号左边</param>
/// <param name="right">=号右边</param>
public static void Assignment<L,R>(L left, R right)
{
var ltype = left.GetType();
List<PropertyInfo> lList = ltype.GetProperties().ToList();
List<PropertyInfo> rList = right.GetType().GetProperties().ToList();
for (int i = 0; i < lList.Count; i++)
{
//根据属性名获得指定的属性对象
PropertyInfo gc = ltype.GetProperty(lList[i].Name);
//验证属性是否可以Set
if (gc.CanWrite == true)
{
try
{
var value = rList.Where(t => t.Name == gc.Name).FirstOrDefault().GetValue(right, null);
//设置属性的值
gc.SetValue(left, value, null);
}
catch
{
}
}
}
}
/// <summary>
/// 将一组List赋值到另一组List(不同类型)
/// </summary>
/// <param name="lift"></param>
/// <param name="right"></param>
public static void Assignment<L, R>(List<L> lift ,List<R> right) where L : new()
{
if (lift == null)
{
lift = new List<L>();
}
foreach (var r in right)
{
var l = new L();
Assignment<L, R>(l, r);
lift.Add(l);
}
}
/// <summary>
/// 将一组List赋值到另一组List(不同类型)多线程
/// </summary>
/// <param name="lift"></param>
/// <param name="right"></param>
public static List<L> AssignmentParallel<L, R>(List<R> right,int Par=5) where L : new()
{
List<L> list = new List<L>();
Parallel.ForEach(right, new ParallelOptions { MaxDegreeOfParallelism = Par }, r =>
{
var l = new L();
Assignment<L, R>(l, r);
list.Add(l);
});
return list;
}
}
}
来源:CSDN
作者:一个五十岁的小屁孩
链接:https://blog.csdn.net/weixin_42629287/article/details/103697627