Skip to content

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.

public sealed class ConstructorGuardDefinitionAttribute : System.Attribute

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.

public ConstructorGuardDefinitionAttribute(System.Type guardType);

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.

public ConstructorGuardDefinitionAttribute(System.Type guardType, string methodName);

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.

public System.Type GuardType { get; }

Property Value

System.Type

ConstructorGuardDefinitionAttribute.MethodName Property

Gets the explicit validation method name, or null when the conventional Validate method name applies.

public string? MethodName { get; }

Property Value

System.String