Initial commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace BracerLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// An object-oriented, extensible base class to represent an enum type.
|
||||
/// </summary>
|
||||
[ExcludeFromCoverage]
|
||||
public abstract class Enumeration : IComparable
|
||||
{
|
||||
public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
|
||||
typeof(T).GetFields(BindingFlags.Public
|
||||
| BindingFlags.Static
|
||||
| BindingFlags.DeclaredOnly)
|
||||
.Select(f => f.GetValue(null))
|
||||
.Cast<T>();
|
||||
|
||||
public string Name { get; private set; }
|
||||
public int Id { get; private set; }
|
||||
|
||||
protected Enumeration(int id, string name) => (Id, Name) = (id, name);
|
||||
|
||||
public override string ToString() => Name;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is not Enumeration otherValue)
|
||||
return false;
|
||||
|
||||
var typeMatches = GetType() == obj.GetType();
|
||||
var valueMatches = Id.Equals(otherValue.Id);
|
||||
|
||||
return typeMatches && valueMatches;
|
||||
}
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(Name, Id);
|
||||
|
||||
public int CompareTo(object obj) => Id.CompareTo(((Enumeration)obj).Id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user