classlib/ExpenseTracker.Domain/Enums/Enumeration.cs
2024-08-07 21:12:02 +03:00

79 lines
1.9 KiB
C#

namespace ExpenseTracker.Domain.Enums;
using System.Reflection;
public abstract class Enumeration<TEnum> : IEquatable<Enumeration<TEnum>>
where TEnum : Enumeration<TEnum>
{
public static Dictionary<int, TEnum> Enumerations { get; private set; } = CreateEnumerations();
protected Enumeration(int value, string name)
{
Value = value;
Name = name;
}
public int Value { get; protected init; }
public string Name { get; protected init; } = string.Empty;
public static TEnum? FromValue(int value)
{
return
Enumerations.TryGetValue(value, out TEnum? enumeration) ?
enumeration :
default;
}
public static TEnum? FromName(string name)
{
return Enumerations.Values.SingleOrDefault(v => v.Name == name);
}
public bool Equals(Enumeration<TEnum>? other)
{
if (other is null)
{
return false;
}
return
GetType() == other.GetType() &&
Value == other.Value;
}
public override bool Equals(object? obj)
{
return
obj is Enumeration<TEnum> other &&
Equals(other);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public override string ToString()
{
return Name;
}
private static Dictionary<int, TEnum> CreateEnumerations()
{
var enumerationType = typeof(TEnum);
var fieldsForType = enumerationType
.GetFields(
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.Where(fieldInfo =>
enumerationType.IsAssignableFrom(fieldInfo.FieldType))
.Select(fieldInfo =>
(TEnum) fieldInfo.GetValue(default)!);
return fieldsForType.ToDictionary(x => x.Value);
}
}