Browse Source

Add Equality comparer for InstalledPackage

pull/109/head
Ionite 1 year ago
parent
commit
723a95f4aa
No known key found for this signature in database
  1. 24
      StabilityMatrix.Core/Helper/PropertyComparer.cs
  2. 3
      StabilityMatrix.Core/Models/InstalledPackage.cs

24
StabilityMatrix.Core/Helper/PropertyComparer.cs

@ -0,0 +1,24 @@
namespace StabilityMatrix.Core.Helper;
public class PropertyComparer<T> : IEqualityComparer<T> where T : class
{
private Func<T, object> Expr { get; set; }
public PropertyComparer(Func<T, object> expr)
{
Expr = expr;
}
public bool Equals(T? x, T? y)
{
if (x == null || y == null) return false;
var first = Expr.Invoke(x);
var second = Expr.Invoke(y);
return first.Equals(second);
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}

3
StabilityMatrix.Core/Models/InstalledPackage.cs

@ -158,6 +158,9 @@ public class InstalledPackage
LibraryPath = System.IO.Path.Combine("Packages", packageFolderName); LibraryPath = System.IO.Path.Combine("Packages", packageFolderName);
} }
public static IEqualityComparer<InstalledPackage> Comparer { get; } =
new PropertyComparer<InstalledPackage>(p => p.Id);
protected bool Equals(InstalledPackage other) protected bool Equals(InstalledPackage other)
{ {
return Id.Equals(other.Id); return Id.Equals(other.Id);

Loading…
Cancel
Save