Class EnumExtensions
- Namespace
- CivitaiSharp.Core.Extensions
- Assembly
- CivitaiSharp.Core.dll
Extension methods for enum types to support API string conversion and validation.
public static class EnumExtensions
- Inheritance
-
EnumExtensions
- Inherited Members
Remarks
This implementation is AOT-compatible and does not use reflection. All enum-to-string mappings are defined in ApiStringRegistry.
Methods
ThrowIfUndefined<TEnum>(TEnum, string?)
Throws an ArgumentOutOfRangeException if the enum value is not defined.
public static TEnum ThrowIfUndefined<TEnum>(this TEnum value, string? parameterName = null) where TEnum : struct, Enum
Parameters
valueTEnumThe enum value to validate.
parameterNamestringThe name of the parameter being validated (provided automatically by the compiler).
Returns
- TEnum
The validated enum value for method chaining.
Type Parameters
TEnumThe enum type.
Exceptions
- ArgumentOutOfRangeException
Thrown if the value is not a defined enum member.
ToApiString<TEnum>(TEnum)
Converts an enum value to its API request parameter string representation.
public static string ToApiString<TEnum>(this TEnum value) where TEnum : struct, Enum
Parameters
valueTEnumThe enum value to convert.
Returns
- string
The API string representation of the enum value.
Type Parameters
TEnumThe enum type.
Remarks
Uses pre-defined frozen dictionary mappings from ApiStringRegistry for optimal performance. Falls back to the enum member name if no mapping exists.
TryParseFromApiString<TEnum>(string?, out TEnum)
Attempts to parse an API string value to the corresponding enum value.
public static bool TryParseFromApiString<TEnum>(string? value, out TEnum result) where TEnum : struct, Enum
Parameters
valuestringThe API string value to parse.
resultTEnumWhen this method returns, contains the parsed enum value if successful.
Returns
- bool
True if parsing succeeded; otherwise, false.
Type Parameters
TEnumThe enum type to parse to.
Remarks
Matches against the API string mappings in ApiStringRegistry first (case-insensitive), then falls back to standard enum parsing by member name.