EnumString<T> provides bidirectional conversion between C# enums and JavaScript string constants. Many JavaScript APIs use string values where C# would use enums (e.g., "readwrite", "arraybuffer", "blob"). EnumString bridges this gap with automatic JSON serialization.
Namespace: SpawnDev.BlazorJS
EnumString<T> wraps a C# enum type T and maps each enum member to a JavaScript string value using [JsonPropertyName] attributes. When serialized to JSON (for JavaScript interop), it outputs the string value. When deserialized, it converts the string back to the enum member.
Use [JsonPropertyName] on each enum member to specify the JavaScript string value:
using System.Text.Json.Serialization;
public enum BinaryType
{
[JsonPropertyName("blob")]
Blob,
[JsonPropertyName("arraybuffer")]
ArrayBuffer,
}EnumString<T> supports implicit conversion from the enum type T, from a string, and back:
// From enum
EnumString<BinaryType> type = BinaryType.ArrayBuffer;
// Serializes as "arraybuffer" in JS
// From string
EnumString<BinaryType> type2 = "blob";
// Maps to BinaryType.Blob
// To string
string jsValue = type.String; // "arraybuffer"
// To enum
BinaryType? enumValue = type.Enum; // BinaryType.ArrayBufferpublic class WebSocket : EventTarget
{
public WebSocket(IJSInProcessObjectReference _ref) : base(_ref) { }
// EnumString property
public EnumString<BinaryType> BinaryType
{
get => JSRef!.Get<EnumString<BinaryType>>("binaryType");
set => JSRef!.Set("binaryType", value);
}
}
// Usage:
using var ws = new WebSocket("wss://example.com");
ws.BinaryType = BinaryType.ArrayBuffer; // Sets "arraybuffer" in JSpublic IDBTransaction Transaction(string storeName, EnumString<IDBTransactionMode> mode)
=> JSRef!.Call<IDBTransaction>("transaction", storeName, mode);
// Usage:
using var tx = db.Transaction("myStore", IDBTransactionMode.ReadWrite);
// Passes "readwrite" to JavaScript| Property | Type | Description |
|---|---|---|
Enum |
T? |
The enum value, or null if the string is not a defined enum member |
String |
string? |
The JavaScript string value |
IsDefined |
bool |
True if the string maps to a defined enum member |
If JavaScript returns a string that does not map to any defined enum member, EnumString still holds the string value, but Enum is null and IsDefined is false:
EnumString<BinaryType> unknown = "somethingNew";
// unknown.Enum == null
// unknown.String == "somethingNew"
// unknown.IsDefined == falseThis allows forward compatibility - your code won't crash when a browser adds new values to an API.
EnumString<T> uses a custom JSON converter (EnumStringConverterFactory). When serialized:
- If the value is
null, JSONnullis written - Otherwise, the
Stringproperty value is written as a JSON string
When deserialized:
- JSON
nullbecomes a nullEnumString<T> - A JSON string is mapped to the matching enum member (or stored as an undefined string if no match)
public enum GPUTextureFormat
{
[JsonPropertyName("rgba8unorm")]
RGBA8Unorm,
[JsonPropertyName("rgba8snorm")]
RGBA8Snorm,
[JsonPropertyName("bgra8unorm")]
BGRA8Unorm,
[JsonPropertyName("rgba16float")]
RGBA16Float,
[JsonPropertyName("rgba32float")]
RGBA32Float,
// ... many more formats
}
// Used in WebGPU API:
public class GPUTexture : JSObject
{
public GPUTexture(IJSInProcessObjectReference _ref) : base(_ref) { }
public EnumString<GPUTextureFormat> Format
=> JSRef!.Get<EnumString<GPUTextureFormat>>("format");
}public enum RTCIceConnectionState
{
[JsonPropertyName("new")]
New,
[JsonPropertyName("checking")]
Checking,
[JsonPropertyName("connected")]
Connected,
[JsonPropertyName("completed")]
Completed,
[JsonPropertyName("disconnected")]
Disconnected,
[JsonPropertyName("failed")]
Failed,
[JsonPropertyName("closed")]
Closed,
}public enum ReadableStreamState
{
[JsonPropertyName("readable")]
Readable,
[JsonPropertyName("closed")]
Closed,
[JsonPropertyName("errored")]
Errored,
}EnumString<T>.ToString() returns the string value (same as String property):
EnumString<BinaryType> type = BinaryType.Blob;
Console.WriteLine(type); // "blob"- Union Types - Multi-type values
- JSObject - Base wrapper class