Replies: 1 comment 1 reply
-
You can define an public readonly struct EquatableEnum<T> : IEquatable<T>, IEquatable<EquatableEnum<T>>, IComparable, IComparable<T>, IComparable<EquatableEnum<T>>, IFormattable
where T : struct, Enum
{
private readonly T _value;
public EquatableEnum(T value) => _value = value;
public T Value => _value;
public static implicit operator T(EquatableEnum<T> v) => v._value;
public static implicit operator EquatableEnum<T>(T v) => new(v);
public bool HasFlag(Enum flag) => _value.HasFlag(flag);
public override int GetHashCode() => _value.GetHashCode();
public override string ToString() => _value.ToString();
public string ToString(string? format) => _value.ToString(format);
public string ToString(string? format, IFormatProvider? provider) => _value.ToString(format);
public bool Equals(T other) => EqualityComparer<T>.Default.Equals(_value, other);
public bool Equals(EquatableEnum<T> other) => Equals(other._value);
public int CompareTo(T other) => Comparer<T>.Default.Compare(_value, other);
public int CompareTo(EquatableEnum<T> other) => CompareTo(other._value);
public int CompareTo(object? target)
{
if (target is EquatableEnum<T> other)
return CompareTo(other);
return _value.CompareTo(target);
}
public override bool Equals(object? obj)
{
if (obj is EquatableEnum<T> other)
return Equals(other);
return _value.Equals(obj);
}
} public enum Token
{
User1Token,
User2Token,
}
WeakReferenceMessenger.Default.Send(new ValueChangedMessage<User1>(user1), (EquatableEnum<Token>)Token.User1Token);
WeakReferenceMessenger.Default.Send(new ValueChangedMessage<User2>(user2), (EquatableEnum<Token>)Token.User2Token); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I don’t want to create new types only to identify different ValueChangedMessage. So token should be very suitable for my requirements. Like this:
I want use enum as token, because it is simple. Like this:
But the token have
where TToken : IEquatable<TToken>
and enum does not meet the requirements. Are there any workarounds here?Beta Was this translation helpful? Give feedback.
All reactions