Wiki

Clone wiki

GoingPostal / StringParameterPolicy

#StringParameterPolicy#

Multicasting attribute for the NotNullAttribute, NotEmptyAttribute or NotWhitespaceAttribute aspects.

The most common use, below, is to add the attribute to the assembly to ensure ALL string parameters on non-private methods are appropriately checked:

#!c#
[assembly: StringParameterPolicy(
AttributeTargetTypes = "YourNameSpaceHereIncludingWildcards.*"
, Policy = StringParameterPolicy.NotNullOrWhiteSpace
, AttributePriority = 1
, AttributeInheritance = MulticastInheritance.Multicast
, AttributeTargetMemberAttributes = GoingPostalExtras.AllExceptPrivate
)]

Policy can take the following value which correspond to the name of the aspect to multicast:

  • NotNull - checks the parameter for null only - does this by adding the NotNullAttribute aspect.
  • NotNullOrEmpty - checks the argument with string.IsNotNullOrEmpty function by adding the NotEmptyAttribute aspect.
  • NotNullOrWhiteSpace - checks the argument with string.IsNotNullOrWhitespace function (N.B. that function is not available in .NET 3.5 but the implementation gives the same result) by adding the NotWhitespaceAttribute aspect.

The above can be customised as per any PostSharp multicasting attribute. E.g. the attribute can be added to a classes or methods instead, you may wish to not check the parameters of internals or protected methods etc.

Also see The Order of Precedence of ContractKillers aspects and Why optional parameters suck.

Updated