Skip to content

Commit

Permalink
Merge pull request #119 from ProductiveRage/master
Browse files Browse the repository at this point in the history
Complete private field and property support for .NET Standard (where …
  • Loading branch information
GregFinzer authored Oct 9, 2018
2 parents 7afca83 + a79b2ce commit 57fa60e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
36 changes: 34 additions & 2 deletions Compare-NET-Objects-Tests/BugTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using KellermanSoftware.CompareNetObjects;
using KellermanSoftware.CompareNetObjectsTests.Attributes;
Expand All @@ -9,7 +8,6 @@
using System.Linq;
using System.Reflection;
using KellermanSoftware.CompareNetObjects.Reports;
using KellermanSoftware.CompareNetObjectsTests.TestClasses.Bal;
using Point = System.Drawing.Point;

#if !NETSTANDARD
Expand Down Expand Up @@ -62,6 +60,40 @@ public void GetPrivatePropertiesNetStandard()
Assert.IsTrue(props.Length > 0);
}

/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/77
/// </summary>
[Test]
public void ComparisonsOfTypesWithPrivateFieldsAreAccurate()
{
var compareLogic = new CompareLogic(new ComparisonConfig { ComparePrivateFields = true });
var result = compareLogic.Compare(new SomethingWithPrivateField(123), new SomethingWithPrivateField(456));
Assert.IsFalse(result.AreEqual);
}

private class SomethingWithPrivateField
{
private readonly int _key;
public SomethingWithPrivateField(int key) { _key = key; }
}

/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/77
/// </summary>
[Test]
public void ComparisonsOfTypesWithPrivatePropertiesAreAccurate()
{
var compareLogic = new CompareLogic(new ComparisonConfig { ComparePrivateProperties = true });
var result = compareLogic.Compare(new SomethingWithPrivateProperty(123), new SomethingWithPrivateProperty(456));
Assert.IsFalse(result.AreEqual);
}

private class SomethingWithPrivateProperty
{
public SomethingWithPrivateProperty(int key) { Key = key; }
private int Key { get; }
}

/// <summary>
/// https://github.com/GregFinzer/Compare-Net-Objects/issues/110
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion Compare-NET-Objects/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IEnumerable<FieldInfo> GetFieldInfo(ComparisonConfig config, Type

FieldInfo[] currentFields;

#if !NETSTANDARD
#if !NETSTANDARD1_3
if (config.ComparePrivateFields && !config.CompareStaticFields)
{
List<FieldInfo> list = new List<FieldInfo>();
Expand Down Expand Up @@ -132,6 +132,12 @@ public static IEnumerable<PropertyInfo> GetPropertyInfo(ComparisonConfig config,

PropertyInfo[] currentProperties;

#if NETSTANDARD1_3
if (!config.CompareStaticProperties)
currentProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
else
currentProperties = type.GetProperties(); //Default is public instance and static
#else
if (config.ComparePrivateProperties && !config.CompareStaticProperties)
currentProperties =
type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Expand All @@ -142,6 +148,7 @@ public static IEnumerable<PropertyInfo> GetPropertyInfo(ComparisonConfig config,
else if (!config.CompareStaticProperties)
currentProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
else
#endif
currentProperties = type.GetProperties(); //Default is public instance and static

if (config.Caching)
Expand Down
4 changes: 4 additions & 0 deletions Compare-NET-Objects/ComparisonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,25 @@ public int MaxStructDepth
#endif
public List<string> MembersToInclude { get; set; }

#if !NETSTANDARD1_3
/// <summary>
/// If true, private properties and fields will be compared. The default is false. Silverlight and WinRT restricts access to private variables.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public bool ComparePrivateProperties { get; set; }
#endif

#if !NETSTANDARD1_3
/// <summary>
/// If true, private fields will be compared. The default is false. Silverlight and WinRT restricts access to private variables.
/// </summary>
#if !NETSTANDARD
[DataMember]
#endif
public bool ComparePrivateFields { get; set; }
#endif

/// <summary>
/// If true, static properties will be compared. The default is true.
Expand Down

0 comments on commit 57fa60e

Please sign in to comment.