You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

42 lines
1.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leit.FrameWork.Reflection
{
public partial class Reflect
{
/// <summary>
/// 相同结构的类之间的赋值(根据属性名称)
/// </summary>
/// <typeparam name="S">原数据类型</typeparam>
/// <typeparam name="T">结果类型</typeparam>
/// <param name="sourceInstance">原数据</param>
/// <param name="resultInstance">结果数据</param>
/// <returns></returns>
public static void CopyPropertyValueByName<S, T>(S sourceInstance, ref T resultInstance)
{
Type type = sourceInstance.GetType();
System.Reflection.PropertyInfo[] sourcePropertys = type.GetProperties();
Type type_Result = typeof(T);
System.Reflection.PropertyInfo[] ResultPropertys = type_Result.GetProperties();
foreach (var Property in sourcePropertys)
{
if (type_Result.GetProperty(Property.Name) != null)
{
type_Result.GetProperty(Property.Name).SetValue(resultInstance, Property.GetValue(sourceInstance));
}
}
}
}
}