Class SmartEnum<T>
Base class for creating type-safe enumeration patterns in domain models. Provides richer behavior than standard C# enums by supporting display names, custom logic, and polymorphism while remaining serialization-friendly via integer IDs.
[RequiresUnreferencedCode("SmartEnum discovery uses reflection to find static fields on the derived type.")]
public abstract class SmartEnum<T> where T : SmartEnum<T>
Type Parameters
TThe concrete enumeration type deriving from this class.
- Inheritance
-
SmartEnum<T>
- Inherited Members
Remarks
This follows the well-known "smart enum" / enumeration pattern used in DDD. Each value is a singleton instance of the derived class, identified by a unique Id and a human-readable Name.
Usage:
public sealed class OrderStatus : SmartEnum<OrderStatus>
{
public static readonly OrderStatus Pending = new(0, nameof(Pending));
public static readonly OrderStatus Processing = new(1, nameof(Processing));
public static readonly OrderStatus Shipped = new(2, nameof(Shipped));
public static readonly OrderStatus Cancelled = new(3, nameof(Cancelled));
private OrderStatus(int id, string name) : base(id, name) { }
}
// Then use it:
var status = OrderStatus.Pending;
var all = OrderStatus.GetAll();
var found = OrderStatus.FromId(1); // OrderStatus.Processing
var byName = OrderStatus.FromName("Shipped"); // OrderStatus.Shipped
Constructors
SmartEnum(int, string)
Initializes a new instance of the SmartEnum<T> class.
protected SmartEnum(int id, string name)
Parameters
idintThe unique integer identifier for this enumeration value.
namestringThe human-readable name for this enumeration value.
Exceptions
- ArgumentNullException
Thrown when
nameis null.- ArgumentException
Thrown when
nameis empty or whitespace.
Properties
Id
Gets the unique integer identifier for this enumeration value.
public int Id { get; }
Property Value
- int
The integer ID used for persistence and serialization.
Name
Gets the human-readable name for this enumeration value.
public string Name { get; }
Property Value
- string
The display name of the enumeration value.
Methods
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
FromId(int)
Gets the enumeration value with the specified id.
public static T FromId(int id)
Parameters
idintThe integer identifier to look up.
Returns
- T
The enumeration value matching the specified ID.
Exceptions
- InvalidOperationException
Thrown when no enumeration value exists with the specified
id.
FromName(string)
Gets the enumeration value with the specified name.
The comparison is case-insensitive.
public static T FromName(string name)
Parameters
namestringThe name to look up.
Returns
- T
The enumeration value matching the specified name.
Exceptions
- ArgumentNullException
Thrown when
nameis null.- InvalidOperationException
Thrown when no enumeration value exists with the specified
name.
GetAll()
Gets all defined values of the enumeration type.
public static IReadOnlyCollection<T> GetAll()
Returns
- IReadOnlyCollection<T>
A read-only collection of all enumeration values.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
TryFromId(int, out T?)
Tries to get the enumeration value with the specified id.
public static bool TryFromId(int id, out T? result)
Parameters
idintThe integer identifier to look up.
resultTWhen this method returns, contains the enumeration value if found; otherwise, null.
Returns
TryFromName(string, out T?)
Tries to get the enumeration value with the specified name.
The comparison is case-insensitive.
public static bool TryFromName(string name, out T? result)
Parameters
namestringThe name to look up.
resultTWhen this method returns, contains the enumeration value if found; otherwise, null.