| Severity | Error |
| Reported by | Metalama.Patterns.Contracts |
| Message | The [<AspectType>] contract cannot be applied to '<Declaration>' because the value range <Range> cannot be satisfied by the type <TargetBasicType>. |
Cause
Range contracts, such as RangeAttribute, GreaterThanOrEqualAttribute, or StrictlyNegativeAttribute, verify that a numeric value lies within a range. At compile time, the contract compares the bounds of the range with the values that the type of the target can hold.
Metalama reports LAMA5001 when no value of the type can satisfy the range, so the check would always fail. This happens when the minimum bound is greater than the largest value of the type, or when the maximum bound is smaller than the smallest value of the type, for instance a range starting at 1024 on a byte, or a strictly negative range on an unsigned type. The message shows the range in interval notation and the name of the type. The contract isn't applied.
In the following example, the Port property is a byte, which can't hold any value between 1024 and 65535.
1using Metalama.Patterns.Contracts;
2
3namespace Doc.LAMA5001.Error;
4
5public class ServerSettings
6{
7 // Error: a byte can't hold any value between 1024 and 65535.
8 [Range( 1024, 65535 )]
Error LAMA5001: The [Range] contract cannot be applied to 'ServerSettings.Port' because the value range [1024, 65535] cannot be satisfied by the type Byte.
9 public byte Port { get; set; }
10}
11
Resolution
Do one of the following:
- Change the type of the target to a numeric type that can hold the values of the range, as in the following example.
- Correct the bounds of the contract if they're wrong.
1using Metalama.Patterns.Contracts;
2
3namespace Doc.LAMA5001.Fixed;
4
5public class ServerSettings
6{
7 // Fixed: a ushort can hold the values of the range.
8 [Range( 1024, 65535 )]
9 public ushort Port { get; set; }
10}
11
1using System;
2using Metalama.Patterns.Contracts;
3
4namespace Doc.LAMA5001.Fixed;
5
6public class ServerSettings
7{
8 private ushort _port;
9
10 // Fixed: a ushort can hold the values of the range.
11 [Range(1024, 65535)]
12 public ushort Port
13 {
14 get
15 {
16 return _port;
17 }
18
19 set
20 {
21 if (value < 1024)
22 {
23 throw new ArgumentOutOfRangeException("value", value, "The 'Port' property must be in the range [1024, 65535].");
24 }
25
26 _port = value;
27 }
28 }
29}
30