Skip to content

Instantly share code, notes, and snippets.

@johannesegger
Last active August 14, 2017 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannesegger/2c12fd331f30b50c06e209217f2528f2 to your computer and use it in GitHub Desktop.
Save johannesegger/2c12fd331f30b50c06e209217f2528f2 to your computer and use it in GitHub Desktop.
Simple implementation of IEquatable<T> if you can't use Equals.Fody
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; }
}
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);
}
}
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);
}
}
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