Class RangeAttribute
- Namespace
- Trellis
- Assembly
- Trellis.Primitives.dll
Specifies the minimum and maximum allowed values for numeric value objects (RequiredInt<TSelf>, RequiredDecimal<TSelf>, RequiredLong<TSelf>).
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class RangeAttribute : Attribute
- Inheritance
-
RangeAttribute
- Inherited Members
- Extension Methods
Examples
[Range(1, 999)]
public partial class Quantity : RequiredInt<Quantity> { }
[Range(0.01, 99.99)]
public partial class UnitPrice : RequiredDecimal<UnitPrice> { }
[Range(0L, 5_000_000_000L)]
public partial class LargeId : RequiredLong<LargeId> { }
Remarks
The source generator reads the constructor arguments at compile time and emits range validation
in the generated TryCreate method. This attribute does not rely on runtime reflection.
Note: This is Trellis.RangeAttribute, not System.ComponentModel.DataAnnotations.RangeAttribute.
If your project imports System.ComponentModel.DataAnnotations, use the fully qualified name
[Trellis.Range(min, max)] to avoid ambiguity.
Constructors
RangeAttribute(double, double)
Specifies a double range. Use for RequiredDecimal<TSelf> with fractional bounds. C# does not allow decimal in attribute constructors, so double is used.
public RangeAttribute(double minimum, double maximum)
Parameters
RangeAttribute(int, int)
Specifies an int range. Use for RequiredInt<TSelf> and RequiredDecimal<TSelf> (whole numbers).
public RangeAttribute(int minimum, int maximum)
Parameters
RangeAttribute(long, long)
Specifies a long range. Use for RequiredLong<TSelf>.
public RangeAttribute(long minimum, long maximum)