diff --git a/Source/ChangeTracking.Tests/MethodToSkipTests.cs b/Source/ChangeTracking.Tests/MethodToSkipTests.cs new file mode 100644 index 0000000..e270ec9 --- /dev/null +++ b/Source/ChangeTracking.Tests/MethodToSkipTests.cs @@ -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()); + } + } +} \ No newline at end of file diff --git a/Source/ChangeTracking/ChangeTrackingFactory.cs b/Source/ChangeTracking/ChangeTrackingFactory.cs index 2f66abd..7df202f 100644 --- a/Source/ChangeTracking/ChangeTrackingFactory.cs +++ b/Source/ChangeTracking/ChangeTrackingFactory.cs @@ -50,6 +50,7 @@ public ICollection AsTrackableCollection(ICollection target) where T : public bool MakeComplexPropertiesTrackable { get; set; } public bool MakeCollectionPropertiesTrackable { get; set; } + public HashSet MethodsToSkip { get; } = new HashSet() { "Equals", "GetType", "ToString", "GetHashCode" }; internal T AsTrackable(T target, ChangeStatus status, Action notifyParentListItemCanceled, ChangeTrackingSettings changeTrackingSettings, Graph graph) where T : class { @@ -61,7 +62,7 @@ internal T AsTrackable(T target, ChangeStatus status, Action notifyParentL } //if T was ICollection it would of gone to one of the other overloads - if (target as ICollection != null) + if (target is ICollection) { throw new InvalidOperationException("Only IList, List and ICollection are supported"); } @@ -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); diff --git a/Source/ChangeTracking/ChangeTrackingProxyGenerationHook.cs b/Source/ChangeTracking/ChangeTrackingProxyGenerationHook.cs index 06eb0ea..c1bf1b5 100644 --- a/Source/ChangeTracking/ChangeTrackingProxyGenerationHook.cs +++ b/Source/ChangeTracking/ChangeTrackingProxyGenerationHook.cs @@ -9,18 +9,15 @@ namespace ChangeTracking { internal class ChangeTrackingProxyGenerationHook : IProxyGenerationHook { - private static HashSet _MethodsToSkip; + private HashSet _MethodsToSkip; private readonly Type _Type; private HashSet _InstanceMethodsToSkip; - static ChangeTrackingProxyGenerationHook() + public ChangeTrackingProxyGenerationHook(Type type, HashSet methodsToSkip) { - _MethodsToSkip = new HashSet { "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(type .GetMethods(bindingFlags)