namespace Prefab.Base; /// /// Base type for rich enumerations that behave like enums but support additional metadata and behavior. /// /// Concrete enumeration type deriving from . public abstract class PrefabEnum where TEnum : PrefabEnum { private static readonly List Instances = []; /// /// Gets the canonical name of the enumeration value. /// public string Name { get; } /// /// Gets the integral value associated with the enumeration value. /// public int Value { get; } /// /// Initializes a new enumeration value and registers it in the global instance list. /// /// Logical name exposed to consumers. /// Numeric identifier for persistence. protected PrefabEnum(string name, int value) { Name = name; Value = value; Instances.Add(this as TEnum ?? throw new InvalidOperationException("PrefabEnum must be instantiated with a derived type.")); } /// /// Gets all declared enumeration values for . /// public static IReadOnlyList List => Instances; /// /// Resolves an enumeration value by its logical name. /// /// The name to match. /// The matching enumeration. /// Thrown when no enumeration value matches the supplied name. public static TEnum FromName(string name) => Instances.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) ?? throw new ArgumentException($"Invalid name: {name}", nameof(name)); /// /// Resolves an enumeration value by its integral value. /// /// The integer value to match. /// The matching enumeration. /// Thrown when no enumeration value matches the supplied value. public static TEnum FromValue(int value) => Instances.FirstOrDefault(x => x.Value == value) ?? throw new ArgumentException($"Invalid value: {value}", nameof(value)); }