namespace StabilityMatrix.Core.Extensions; public static class EnumConversionExtensions { public static T? ConvertTo(this Enum value) where T : Enum { var type = value.GetType(); var fieldInfo = type.GetField(value.ToString()); // Get the string value attributes var attribs = fieldInfo?.GetCustomAttributes(typeof(ConvertToAttribute), false) as ConvertToAttribute[]; // Return the first if there was a match. return attribs?.Length > 0 ? attribs[0].ConvertToEnum : default; } } [AttributeUsage(AttributeTargets.Field)] public sealed class ConvertToAttribute : Attribute where T : Enum { public T ConvertToEnum { get; } public ConvertToAttribute(T toEnum) { ConvertToEnum = toEnum; } }