25 lines
933 B
C#
25 lines
933 B
C#
using System.Collections;
|
|
using System.Reflection;
|
|
|
|
namespace AyCode.Core.Extensions;
|
|
|
|
public static class PropertyHelper
|
|
{
|
|
public static TDestination CopyPublicValueTypeProperties<TSource, TDestination>(TSource src, TDestination dest)
|
|
{
|
|
if (src == null || dest == null) return dest;
|
|
|
|
var srcProps = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead);// && !typeof(IEnumerable).IsAssignableFrom(p.PropertyType));
|
|
|
|
foreach (var sp in srcProps)
|
|
{
|
|
var dp = typeof(TDestination).GetProperty(sp.Name, BindingFlags.Public | BindingFlags.Instance);
|
|
if (dp == null || !dp.CanWrite) //|| typeof(IEnumerable).IsAssignableFrom(dp.PropertyType)) //dp.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
|
|
continue;
|
|
|
|
dp.SetValue(dest, sp.GetValue(src));
|
|
}
|
|
|
|
return dest;
|
|
}
|
|
} |