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
20 changes: 20 additions & 0 deletions Source/ChangeTracking.Tests/MethodToSkipTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentAssertions;
using Xunit;

namespace ChangeTracking.Tests
{
public class MethodToSkipTests
{
[Fact]
public void SkipMethods()
{
ChangeTrackingFactory.Default.MethodsToSkip.Remove(nameof(Equals));
ChangeTrackingFactory.Default.MethodsToSkip.Remove(nameof(GetHashCode));

var order = Helper.GetOrder();
Order trackable = order.AsTrackable();
trackable.Should().Be(order);
trackable.GetHashCode().Should().Be(order.GetHashCode());
}
}
}
5 changes: 3 additions & 2 deletions Source/ChangeTracking/ChangeTrackingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public ICollection<T> AsTrackableCollection<T>(ICollection<T> target) where T :

public bool MakeComplexPropertiesTrackable { get; set; }
public bool MakeCollectionPropertiesTrackable { get; set; }
public HashSet<string> MethodsToSkip { get; } = new HashSet<string>() { "Equals", "GetType", "ToString", "GetHashCode" };

internal T AsTrackable<T>(T target, ChangeStatus status, Action<T> notifyParentListItemCanceled, ChangeTrackingSettings changeTrackingSettings, Graph graph) where T : class
{
Expand All @@ -61,7 +62,7 @@ internal T AsTrackable<T>(T target, ChangeStatus status, Action<T> notifyParentL
}

//if T was ICollection<T> it would of gone to one of the other overloads
if (target as ICollection != null)
if (target is ICollection)
{
throw new InvalidOperationException("Only IList<T>, List<T> and ICollection<T> are supported");
}
Expand Down Expand Up @@ -118,7 +119,7 @@ private ProxyGenerationOptions GetOptions(Type type)
{
ProxyGenerationOptions CreateOptions(Type createOptionsForType) => new ProxyGenerationOptions
{
Hook = new ChangeTrackingProxyGenerationHook(createOptionsForType),
Hook = new ChangeTrackingProxyGenerationHook(createOptionsForType, MethodsToSkip),
Selector = _Selector
};
return _Options.GetOrAdd(type, CreateOptions);
Expand Down
11 changes: 4 additions & 7 deletions Source/ChangeTracking/ChangeTrackingProxyGenerationHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ namespace ChangeTracking
{
internal class ChangeTrackingProxyGenerationHook : IProxyGenerationHook
{
private static HashSet<string> _MethodsToSkip;
private HashSet<string> _MethodsToSkip;
private readonly Type _Type;
private HashSet<MethodInfo> _InstanceMethodsToSkip;

static ChangeTrackingProxyGenerationHook()
public ChangeTrackingProxyGenerationHook(Type type, HashSet<string> methodsToSkip)
{
_MethodsToSkip = new HashSet<string> { "Equals", "GetType", "ToString", "GetHashCode" };
}
_Type = type ?? throw new ArgumentNullException(nameof(type));
_MethodsToSkip = methodsToSkip ?? throw new ArgumentNullException(nameof(methodsToSkip));

public ChangeTrackingProxyGenerationHook(Type type)
{
_Type = type;
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
_InstanceMethodsToSkip = new HashSet<MethodInfo>(type
.GetMethods(bindingFlags)
Expand Down