ConstructorGuardKind
NexusLabs.Needlr.Generators¶
ConstructorGuardKind Enum¶
Identifies a built-in constructor guard clause that ConstructorGuardAttribute can request for a field participating in generated constructor generation.
Fields¶
None 0
No guard. When applied explicitly, suppresses an applicable class-level automatic guard default for this field without affecting other guards.
NotNull 1
Throws System.ArgumentNullException when the value is null.
NotNullOrEmpty 2
Throws System.ArgumentNullException when the value is null, or System.ArgumentException when it is an empty string.
NotNullOrWhiteSpace 3
Throws System.ArgumentNullException when the value is null, or System.ArgumentException when it is empty or consists only of white-space characters.
Example¶
public partial class TenantService
{
[ConstructorGuard(ConstructorGuardKind.NotNullOrWhiteSpace)]
private readonly string _tenantName;
}
// Generated:
// public TenantService(string tenantName)
// {
// ArgumentException.ThrowIfNullOrWhiteSpace(tenantName);
// _tenantName = tenantName;
// }
Remarks¶
Built-in guards emit the equivalent standard .NET guard-clause call (for example
ArgumentNullException.ThrowIfNull(object?, string?)) directly
in the generated constructor, so the reported exception type and
ParamName exactly match what the same call would produce if hand-written.
Applicability of a given kind to a given field type is validated by an analyzer; for example NotNullOrWhiteSpace only applies to System.String -compatible fields.