Optimize performance with Source Code Generation
mainFor mission-critical scenarios where maximum speed is required, FastEnum v2 offers a source code generation API. This avoids the overhead of internal caching by generating specialized code at compile time.
To use it:
- Annotate your target enum type with
[FastEnum<TEnum>]. - Define a
partial classto act as the placeholder for the generated code. - Pass the placeholder class as a generic argument to the FastEnum methods.
Supported methods with source generation:
FastEnum.ToString<TEnum, TBooster>(TEnum value)FastEnum.IsDefined<TEnum, TBooster>(TEnum value)FastEnum.Parse<TEnum, TBooster>(string value)FastEnum.TryParse<TEnum, TBooster>(string value, out TEnum result)
[FastEnum<HttpStatusCode>] // Annotate target enum type
partial class HttpStatusCodeBooster // Placeholder for source code generation
{ }
var x1 = FastEnum.ToString<HttpStatusCode, HttpStatusCodeBooster>(HttpStatusCode.OK);
var x2 = FastEnum.IsDefined<HttpStatusCode, HttpStatusCodeBooster>(HttpStatusCode.OK);
var x3 = FastEnum.Parse<HttpStatusCode, HttpStatusCodeBooster>("OK");
var x4 = FastEnum.TryParse<HttpStatusCode, HttpStatusCodeBooster>("OK", out var value);