I have the following classes:
public partial class AuthorizationSetObject
{
public AuthorizationObjectList AuthorizationObjects { get; set; }
}
public partial class AuthorizationObject
{
public string Text { get; set; }
}
public partial class AuthorizationObjectList : List<AuthorizationObject>
{
}
I need now a deep copy of AuthorizationSetObject. How can I do this?
I tried it like this:
public static bool CopyProperties(object source, object target)
{
var customerType = target.GetType();
foreach (var prop in source.GetType().GetProperties())
{
var propGetter = prop.GetGetMethod();
if (propGetter != null)
{
PropertyInfo pi = customerType.GetProperty(prop.Name);
if (pi != null)
{
var propSetter = pi.GetSetMethod();
if (propSetter != null)
{
var valueToSet = propGetter.Invoke(source, null);
propSetter.Invoke(target, new[] { valueToSet });
}
}
}
}
return true;
}
The problem is, that the AuthorizationObjectList is not a real deep copy. If I change the property "Text" from the target after the deep copy, the "Text" from source is changed a well.
Probably I need an implementation like "pi.PropertyType.BaseType.IsGenericType" and then do something else...but what??
Does anybody has an idea?