sharee.bike-App/SharedBusinessLogic/Repository/Response/ComparableDictionary.cs
2024-04-09 12:53:23 +02:00

32 lines
1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace ShareeBike.Repository.Response
{
/// <summary>
/// Holds a dictionary of comparable elements.
/// </summary>
/// <typeparam name="S">Type of the element.</typeparam>
[DataContract]
public class ComparableDictionary<S> : Dictionary<string, S>, IEquatable<ComparableDictionary<S>>
{
public bool Equals(ComparableDictionary<S> other)
{
return this.OrderBy(x => x.Key).SequenceEqual(other.OrderBy(x => x.Key));
}
public static bool operator ==(ComparableDictionary<S> first, ComparableDictionary<S> second)
=> JsonConvert.SerializeObject(first) == JsonConvert.SerializeObject(second);
public static bool operator !=(ComparableDictionary<S> first, ComparableDictionary<S> second)
=> !(first == second);
public override bool Equals(object obj) => obj is ComparableDictionary<S> target && target == this;
public override int GetHashCode() => JsonConvert.SerializeObject(this).GetHashCode();
}
}