Multi-Platform Package Manager for Stable Diffusion
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

24 lines
787 B

namespace StabilityMatrix.Core.Extensions;
public static class EnumConversionExtensions
{
public static T? ConvertTo<T>(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<T>), false) as ConvertToAttribute<T>[];
// Return the first if there was a match.
return attribs?.Length > 0 ? attribs[0].ConvertToEnum : default;
}
}
[AttributeUsage(AttributeTargets.Field)]
public sealed class ConvertToAttribute<T> : Attribute where T : Enum
{
public T ConvertToEnum { get; }
public ConvertToAttribute(T toEnum)
{
ConvertToEnum = toEnum;
}
}