copy of nested properties works now

This commit is contained in:
2021-11-17 13:33:12 +01:00
parent 936782935a
commit 8d9d52a08a
3 changed files with 76 additions and 31 deletions
+1 -30
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@@ -238,36 +239,6 @@ namespace zero.Core.Utils
}
public static T CopyProperties<T>(T source, T target, params string[] exceptions)
{
Type sourceType = source.GetType();
Type stringType = typeof(System.String);
foreach (PropertyInfo sourceProperty in sourceType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
Console.WriteLine("key: " + sourceProperty.Name);
if (!sourceProperty.CanWrite || exceptions.Contains(sourceProperty.Name, StringComparer.InvariantCultureIgnoreCase))
{
continue;
}
object sourcePropertyValue = sourceProperty.GetValue(source, null);
if (sourceProperty.PropertyType.IsValueType || sourceProperty.PropertyType.IsEnum || sourceProperty.PropertyType.Equals(stringType) || sourcePropertyValue == null || sourcePropertyValue is ICollection)
{
sourceProperty.SetValue(target, sourcePropertyValue, null);
}
else if (sourceProperty.PropertyType.IsClass)
{
sourceProperty.SetValue(target, CopyProperties(sourcePropertyValue, sourceProperty.GetValue(target, null)), null);
}
}
return target;
}
private static string CombineKey(string sep, string key1, string key2)
{
if (String.IsNullOrEmpty(key1))