ConstructorNullGuardMode
NexusLabs.Needlr.Generators¶
ConstructorNullGuardMode Enum¶
Controls whether GenerateConstructorAttribute automatically emits null guards for eligible constructor parameters.
Fields¶
None 0
No automatic null guards are emitted. Field assignments are generated as-is. This is the default used by the parameterless GenerateConstructorAttribute constructor.
NonNullableReferences 1
Automatically emits a null guard for every eligible non-nullable reference-type field. Nullable reference-type fields and value-type fields are left unguarded.
Example¶
[GenerateConstructor(ConstructorNullGuardMode.NonNullableReferences)]
public partial class UserService
{
private readonly IRepository _repository;
}
// Generated:
// public UserService(IRepository repository)
// {
// ArgumentNullException.ThrowIfNull(repository);
// _repository = repository;
// }
Remarks¶
This mode only ever affects reference-type fields. Value-type fields (including nullable value types) never receive an automatic null guard, and nullable reference-type fields are always excluded from the automatic default because a nullable annotation is an explicit declaration that null is a valid value.
A field can opt out of the class-level default for itself by applying
[ConstructorGuard(ConstructorGuardKind.None)], regardless of the mode
configured here.