2025-06-12 18:41:46 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Extensions;
|
|
|
|
|
|
|
|
|
|
|
|
public static class ObjectExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对象转换,将source对象上的所有属性的值,都转换到target对象上
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
|
/// <param name="target"></param>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
public static void CopyTo<T>(this Object source ,T target)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sourceType = source.GetType();
|
|
|
|
|
|
var targetType = target.GetType();
|
|
|
|
|
|
var sourceProperties = sourceType.GetProperties();
|
|
|
|
|
|
foreach (PropertyInfo sourceProperty in sourceProperties)
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
|
|
|
|
|
|
if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType)
|
|
|
|
|
|
{
|
|
|
|
|
|
object value = sourceProperty.GetValue(source, null);
|
|
|
|
|
|
targetProperty.SetValue(target, value, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-20 18:53:29 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建一个泛型对象,将source对象上的所有属性的值,都转换到新创建对象上
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="source"></param>
|
|
|
|
|
|
/// <param name="target"></param>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2025-06-22 21:00:06 +08:00
|
|
|
|
public static T NewTo<T>(this Object source ) where T : new()
|
2025-06-20 18:53:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
T target = new T();
|
|
|
|
|
|
var sourceType = source.GetType();
|
|
|
|
|
|
var targetType = target.GetType();
|
|
|
|
|
|
var sourceProperties = sourceType.GetProperties();
|
|
|
|
|
|
foreach (PropertyInfo sourceProperty in sourceProperties)
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
|
|
|
|
|
|
if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType)
|
|
|
|
|
|
{
|
|
|
|
|
|
object value = sourceProperty.GetValue(source, null);
|
|
|
|
|
|
targetProperty.SetValue(target, value, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-22 21:00:06 +08:00
|
|
|
|
return target;
|
2025-06-20 18:53:29 +08:00
|
|
|
|
}
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|