ConstructorGuardDefinitionAttribute
NexusLabs.Needlr.Generators¶
ConstructorGuardDefinitionAttribute Class¶
Declares that an application-defined attribute type is an alias for a constructor guard, so it can be applied to fields directly instead of ConstructorGuardAttribute.
Inheritance System.Object 🡒 System.Attribute 🡒 ConstructorGuardDefinitionAttribute
Example¶
[ConstructorGuardDefinition(typeof(CollectionNotEmptyGuard))]
[AttributeUsage(AttributeTargets.Field)]
public sealed class CollectionNotEmptyAttribute : Attribute
{
}
public partial class OrderService
{
[CollectionNotEmpty]
private readonly IReadOnlyCollection<Order> _orders;
}
// Generated call: CollectionNotEmptyGuard.Validate(orders, nameof(orders));
Remarks¶
Apply this meta-attribute to your own System.Attribute-derived field attribute type. When the generator finds your attribute applied to a field, it resolves the guard from this meta-attribute and treats the field exactly as if ConstructorGuardAttribute had been applied directly with the same guard type -- including as a positive trigger for constructor generation.
This works whether your alias attribute type is declared in the same project as the field that uses it, or in a referenced assembly.
Constructors¶
ConstructorGuardDefinitionAttribute(Type) Constructor¶
Initializes a new instance of the ConstructorGuardDefinitionAttribute
class for a custom guard type using the conventional Validate method.
Parameters¶
guardType System.Type
A type exposing an accessible static method named
Validate, compatible with void Validate(T value, string parameterName).
ConstructorGuardDefinitionAttribute(Type, string) Constructor¶
Initializes a new instance of the ConstructorGuardDefinitionAttribute class for a custom guard type and an explicit validation method name.
Parameters¶
guardType System.Type
A type exposing an accessible static method matching
methodName, compatible with
void Validate(T value, string parameterName).
methodName System.String
The name of the static validation method to call, typically supplied via nameof.
Properties¶
ConstructorGuardDefinitionAttribute.GuardType Property¶
Gets the custom guard type associated with the decorated alias attribute.
Property Value¶
ConstructorGuardDefinitionAttribute.MethodName Property¶
Gets the explicit validation method name, or null when the
conventional Validate method name applies.