You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SmartEnum] generated sealed type conflicts with source generators that add virtual members #24
'OrderStatus.Validate()' is a new virtual member in sealed type 'OrderStatus'
'OrderStatus.ValidateAsync(CancellationToken)' is a new virtual member in sealed type 'OrderStatus'
From what I can tell, the SmartEnum source generator makes the generated partial type sealed, while Validly generates virtual Validate/ValidateAsync members for [Validatable] classes.
virtual in a sealed type: A source generator cannot see the output of another source generator. Validly therefore sees a class without sealed and generates virtual members, while this library adds sealed in its own partial part. You can fix this by declaring the class as public sealed partial class OrderStatus. Validly then generates the members without virtual.
Analyzer error TTRESG001 for _validationContext: After the first fix, the analyzer reports the field _validationContext that Validly generates. This error is intended. The items of a Smart Enum are static instances that all threads share, so a Smart Enum must not have mutable instance state. Validly writes the validation context into that field on every validation. Two concurrent validations of OrderStatus.Pending would overwrite the context of each other.
For this reason, [Validatable] is not supported on a Smart Enum. It is also not needed: an item is valid by construction, and an invalid key is rejected by Get and TryGet.
Activity
PawelGerr commentedon Oct 1, 2026
There are two separate problems here.
virtualin a sealed type: A source generator cannot see the output of another source generator. Validly therefore sees a class withoutsealedand generatesvirtualmembers, while this library addssealedin its own partial part. You can fix this by declaring the class aspublic sealed partial class OrderStatus. Validly then generates the members withoutvirtual.Analyzer error TTRESG001 for
_validationContext: After the first fix, the analyzer reports the field_validationContextthat Validly generates. This error is intended. The items of a Smart Enum are static instances that all threads share, so a Smart Enum must not have mutable instance state. Validly writes the validation context into that field on every validation. Two concurrent validations ofOrderStatus.Pendingwould overwrite the context of each other.For this reason,
[Validatable]is not supported on a Smart Enum. It is also not needed: an item is valid by construction, and an invalid key is rejected byGetandTryGet.christophdebaene commentedon Oct 6, 2026
Thank you very much for your quick answer!