PostSharpAPI ReferencePost­Sharp.​Patterns.​ContractsString­Length­Attribute
Open sandboxFocus

StringLengthAttribute Class

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
[HasConstraint]
public sealed class StringLengthAttribute : LocationContractAttribute, ILocationLevelAspect, IAspect
Remarks

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

  • if there is no minimum specified, then the error message is identified by StringLengthMaxErrorMessage and can use additional argument {4} to refer to the maximum value specified,
  • or if there is maximum is equal to MaxValue, then the error message is identified by StringLengthMinErrorMessage and can use additional argument {4} to refer to the minimum value specified,
  • otherwise, the error message is identified by StringLengthRangeErrorMessage and can use additional arguments {4} to refer to the minimum value specified and {5} to refer to the maximum value specified.

Constructors

Name Description
StringLengthAttribute(Int32)

Initializes a new StringLengthAttribute and specifies the maximum length.

StringLengthAttribute(Int32, Int32)

Initializes a new StringLengthAttribute 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 unformatted error message as defined by the instance.

GetErrorMessageArguments()

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

ValidateValue(String, String, LocationKind, LocationValidationContext)
ValidateValueDynamic(Object, String, LocationKind, LocationValidationContext)

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