Burtsev-Alexey/net-object-deep-copy

Not cloning base type fields

Closed this issue · 7 comments

Hi,

I recently saw a link to your cloning methods from stackoverflow and decided to try them out. I had my own methods that used a similar method, but I was creating my deep clone instances using Activator instead of MemberwiseClone, and your way has less side effects. However, I noticed that it only clones the private fields of the original object type and not fields of the objects base type(s) (if any).

The following change will take care of that:

Type reflectingType = originalType
while (reflectingType != null)
{
    foreach (.....
    {
        // nothing changed here
    }
    reflectingType = reflectingType.BaseType
}

Again, great work on this deep-clone, love it.

Tim

That;s strange i use flatten hierarchy when getting fields, I will check this out, thank for report.

FlattenHierarchy will only return public and protected fields, not private. http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx, so an ever better approach then above would be to traverse all fields for the type and base type(s) and remove any duplicates...

Forgor recursion for baseType -)

Now recursive.

FlattenHierarchy only work for static members, so its redundant

FlattenHierarchy only work for static members, so its redundant

You are right.