Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DeepCloner/Helpers/DeepClonerExprGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ private static object GenerateProcessMethod(Type type, bool unboxStruct)
if (tp.Name == "ContextBoundObject") break;
#endif

fi.AddRange(tp.GetDeclaredFields());
tp = tp.BaseType();
// Avoid to copy property change event handlers (or should we avoid to copy event handlers at all?)
var fields = tp.GetDeclaredFields().Where(x => !x.IsPropertyChangeEventHandler());
fi.AddRange(fields);
tp = tp.BaseType();
}
while (tp != null);

Expand Down
34 changes: 28 additions & 6 deletions DeepCloner/Helpers/DeepClonerGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Linq;

namespace Force.DeepCloner.Helpers
Expand Down Expand Up @@ -33,8 +34,10 @@ private static object CloneClassRoot(object obj)
if (cloner == null)
return obj;

return cloner(obj, new DeepCloneState());
}
var result = cloner(obj, new DeepCloneState());
result = RemovePropertyChangeEventHandler(result);
return result;
}

internal static object CloneClassInternal(object obj, DeepCloneState state)
{
Expand All @@ -52,10 +55,29 @@ internal static object CloneClassInternal(object obj, DeepCloneState state)
if (knownRef != null)
return knownRef;

return cloner(obj, state);
}

private static T CloneStructInternal<T>(T obj, DeepCloneState state) // where T : struct
var result = cloner(obj, state);
result = RemovePropertyChangeEventHandler(result);
return result;
}

/// <summary>
/// Clears fields that store <see cref="PropertyChangingEventHandler"/> or <see cref="PropertyChangedEventHandler"/> instances.
/// </summary>
private static object RemovePropertyChangeEventHandler(object o)
{
var t = o.GetType();
var fields = o.GetType().GetAllFieldsIncludingBaseTypes();
foreach (var fieldInfo in fields)
{
if (fieldInfo.IsPropertyChangeEventHandler())
{
fieldInfo.SetValue(o, null);
}
}
return o;
}

private static T CloneStructInternal<T>(T obj, DeepCloneState state) // where T : struct
{
// no loops, no nulls, no inheritance
var cloner = GetClonerForValueType<T>();
Expand Down
31 changes: 29 additions & 2 deletions DeepCloner/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -50,8 +52,33 @@ public static FieldInfo[] GetAllFields(this Type t)
return t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
#endif
}

public static PropertyInfo[] GetPublicProperties(this Type t)

/// <summary>
/// Returns all fields declared on the specified type and its base types.
/// </summary>
/// <param name="t"></param>
internal static FieldInfo[] GetAllFieldsIncludingBaseTypes(this Type t)
{
var fields = new List<FieldInfo>();
Type currentType = t;
do
{
fields.AddRange(currentType.GetAllFields());
currentType = currentType.BaseType();
}
while (currentType != null);
return fields.ToArray();
}

/// <summary>
/// Indicates whether the field type is <see cref="PropertyChangingEventHandler"/> or <see cref="PropertyChangedEventHandler"/>.
/// </summary>
internal static bool IsPropertyChangeEventHandler(this FieldInfo f)
{
return f.FieldType == typeof(PropertyChangingEventHandler) || f.FieldType == typeof(PropertyChangedEventHandler);
}

public static PropertyInfo[] GetPublicProperties(this Type t)
{
#if NETCORE
return t.GetTypeInfo().DeclaredProperties.ToArray();
Expand Down