Class ApiStringRegistry
- Namespace
- CivitaiSharp.Core.Extensions
- Assembly
- CivitaiSharp.Core.dll
Provides AOT-compatible, reflection-free mappings between enum values and their API string representations. All mappings use frozen dictionaries for optimal runtime performance.
public static class ApiStringRegistry
- Inheritance
-
ApiStringRegistry
- Inherited Members
Remarks
This registry eliminates the need for reflection when converting enums to API strings, making the library compatible with AOT compilation and trimming.
External libraries (like CivitaiSharp.Sdk) can register additional enum mappings using Register<TEnum>(Dictionary<TEnum, string>) at startup.
Methods
GetApiString(Enum)
Converts an enum value to its API string representation (non-generic overload for AOT compatibility). This method uses pattern matching instead of reflection for full AOT support.
public static string GetApiString(Enum value)
Parameters
valueEnumThe enum value to convert.
Returns
- string
The API string representation, or the enum member name if no mapping exists.
Register<TEnum>(Dictionary<TEnum, string>)
Registers an enum type with its API string mappings. This method is thread-safe and can be called at any time, though it's recommended to call it during application startup for best performance.
public static void Register<TEnum>(Dictionary<TEnum, string> toApiString) where TEnum : struct, Enum
Parameters
toApiStringDictionary<TEnum, string>Dictionary mapping enum values to their API string representations.
Type Parameters
TEnumThe enum type to register.
Exceptions
- ArgumentNullException
Thrown when
toApiStringis null.
ToApiString<TEnum>(TEnum)
Converts an enum value to its API string representation.
public static string ToApiString<TEnum>(TEnum value) where TEnum : struct, Enum
Parameters
valueTEnumThe enum value to convert.
Returns
- string
The API string representation, or the enum member name if no mapping exists.
Type Parameters
TEnumThe enum type.
TryParseFromApiString<TEnum>(string?, out TEnum)
Attempts to parse an API string 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.