Open sandboxFocus

Class StringLengthAttribute

Custom attribute that, when added to a field, property or parameter, throws an ArgumentException if the target is assigned a string of invalid length. Null strings are accepted and do not throw an exception.

Inheritance
StringLengthAttribute
Namespace: PostSharp.Patterns.Contracts
Assembly: PostSharp.Patterns.Common.dll
Syntax
public sealed class StringLengthAttribute : LocationContractAttribute, ILocationLevelAspect, IAspect
Remarks

Depending on the supplied constructor arguments, one of the following holds:

<ul><li>
    if there is no minimum specified, then the error message is identified by <xref href="PostSharp.Patterns.Contracts.ContractLocalizedTextProvider.StringLengthMaxErrorMessage" data-throw-if-not-resolved="false"></xref>
    and can use additional argument <code>{4}</code> to refer to the maximum value specified,
</li><li>
    or if the maximum is equal to <xref href="System.Int32.MaxValue" data-throw-if-not-resolved="false"></xref>, then the error message is identified by <xref href="PostSharp.Patterns.Contracts.ContractLocalizedTextProvider.StringLengthMinErrorMessage" data-throw-if-not-resolved="false"></xref>
    and can use additional argument <code>{4}</code> to refer to the minimum value specified,
</li><li>
    otherwise, the error message is identified by <xref href="PostSharp.Patterns.Contracts.ContractLocalizedTextProvider.StringLengthRangeErrorMessage" data-throw-if-not-resolved="false"></xref>
    and can use additional arguments <code>{4}</code> to refer to the minimum value specified and <code>{5}</code> to refer to the maximum value specified.
</li></ul></p>

Constructors

Name Description
StringLengthAttribute(int)

Initializes a new instance of the StringLengthAttribute class and specifies the maximum length.

StringLengthAttribute(int, int)

Initializes a new instance of the StringLengthAttribute class and specifies the maximum length and the minimum length.

Properties

Name Description
MaximumLength

Gets the maximum length.

MinimumLength

Gets the minimum length.

Methods

Name Description
GetErrorMessage()

Gets the unformatted error message as defined by the instance.

GetErrorMessageArguments()

Gets the values of the additional arguments for the error message associated with a particular instance of LocationContractAttribute. These arguments can be referenced from the error message string by {4}, {5}, and so on.

ValidateValueDynamic(object, string, LocationKind, LocationValidationContext)

Validates a weakly-typed (boxed) value, typically the value of a dependency property or an attached property.