Table of Contents

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

value Enum

The 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

toApiString Dictionary<TEnum, string>

Dictionary mapping enum values to their API string representations.

Type Parameters

TEnum

The enum type to register.

Exceptions

ArgumentNullException

Thrown when toApiString is 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

value TEnum

The enum value to convert.

Returns

string

The API string representation, or the enum member name if no mapping exists.

Type Parameters

TEnum

The 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

value string

The API string value to parse.

result TEnum

When this method returns, contains the parsed enum value if successful.

Returns

bool

True if parsing succeeded; otherwise, false.

Type Parameters

TEnum

The enum type.