Last active
August 14, 2017 19:45
-
-
Save johannesegger/2c12fd331f30b50c06e209217f2528f2 to your computer and use it in GitHub Desktop.
Simple implementation of IEquatable<T> if you can't use Equals.Fody
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChannelIdentifier : GenericEquatable<ChannelIdentifier> | |
{ | |
public ChannelIdentifier(LiveId liveId, SampleRateId sampleRateId, int channelId) | |
: base(EqualityComparer.Create((ChannelIdentifier p) => new { p.LiveId, p.SampleRateId, p.ChannelId })) | |
{ | |
this.LiveId = liveId; | |
this.SampleRateId = sampleRateId; | |
this.ChannelId = channelId; | |
} | |
public LiveId LiveId { get; } | |
public SampleRateId SampleRateId { get; } | |
public int ChannelId { get; } | |
} | |
public class LiveId : GenericEquatable<LiveId> | |
{ | |
public LiveId(int liveObjectIndex) | |
: base(EqualityComparer.Create((LiveId p) => p.LiveObjectIndex)) | |
{ | |
this.LiveObjectIndex = liveObjectIndex; | |
} | |
public int LiveObjectIndex { get; } | |
} | |
public class SampleRateId : GenericEquatable<SampleRateId> | |
{ | |
public SampleRateId(int id) | |
: base(EqualityComparer.Create((SampleRateId p) => p.Id)) | |
{ | |
this.Id = id; | |
} | |
public int Id { get; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class GenericEquatable<T> : IEquatable<T> | |
where T : GenericEquatable<T> | |
{ | |
private readonly IEqualityComparer<T> equalityComparer; | |
protected GenericEquatable(IEqualityComparer<T> equalityComparer) | |
{ | |
this.equalityComparer = equalityComparer; | |
} | |
public bool Equals(T other) | |
{ | |
return this.equalityComparer.Equals((T)this, other); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) | |
{ | |
return false; | |
} | |
if (ReferenceEquals(this, obj)) | |
{ | |
return true; | |
} | |
if (obj.GetType() != this.GetType()) | |
{ | |
return false; | |
} | |
return this.Equals((T)obj); | |
} | |
public override int GetHashCode() | |
{ | |
return this.equalityComparer.GetHashCode((T)this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EqualityComparer | |
{ | |
public static IEqualityComparer<TObj> Create<TObj, TKey>(Func<TObj, TKey> keySelector, IEqualityComparer<TKey> keyComparer) | |
{ | |
return new GenericEqualityComparer<TObj>( | |
(x, y) => keyComparer.Equals(keySelector(x), keySelector(y)), | |
obj => keyComparer.GetHashCode(keySelector(obj))); | |
} | |
public static IEqualityComparer<TObj> Create<TObj, TKey>(Func<TObj, TKey> keySelector) | |
{ | |
return Create(keySelector, EqualityComparer<TKey>.Default); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericEqualityComparer<T> : IEqualityComparer<T> | |
{ | |
private readonly Func<T, T, bool> equals; | |
private readonly Func<T, int> getHashCode; | |
public GenericEqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode) | |
{ | |
this.equals = equals; | |
this.getHashCode = getHashCode; | |
} | |
public bool Equals(T x, T y) | |
{ | |
return this.equals(x, y); | |
} | |
public int GetHashCode(T obj) | |
{ | |
return this.getHashCode(obj); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment