| Severity | Error |
| Reported by | PostSharp |
| Message | Error in the [NamingConvention] constraint applied to <Type>: the string "<Pattern>" is not a valid pattern. It should contain a "*" or start with the "regex:" prefix. |
Cause
The NamingConventionAttribute constraint, applied to a class or an interface, checks that the names of this type and of the types derived from it match a pattern. The pattern is one of:
- a wildcard pattern, which contains at least one
*, such as*Repository; - a regular expression, with the
regex:prefix, such asregex:^I[A-Z].
PostSharp reports AR0109 when the pattern is empty, or is a plain name without a * and without the regex: prefix. PostSharp then does not check the names.
The constraint is inherited by the derived classes and by the classes that implement the interface, so PostSharp reports AR0109 once for the type that has the attribute and once for each of these types, at the location of the type rather than of the attribute. <Type> is the type being checked.
In the following example, the pattern of the constraint on IRepository is Repository instead of *Repository.
using System;
using System.Collections.Generic;
using PostSharp.Constraints;
namespace Errors.AR0109_Error;
public record Customer( int Id, string Name );
// "Repository" is neither a wildcard pattern nor a regular expression.
[NamingConvention( "Repository" )]
public interface IRepository
{
Customer? Find( int id );
}
public class CustomerRepository : IRepository
{
private readonly Dictionary<int, Customer> customers = new() { [1] = new Customer( 1, "Contoso" ) };
public Customer? Find( int id ) => this.customers.GetValueOrDefault( id );
}
public static class Program
{
public static void Main()
{
Console.WriteLine( $"Customer 1: {new CustomerRepository().Find( 1 )?.Name}" );
}
}
error AR0109: AR0109_Error.cs(11,18): Error in the [NamingConvention] constraint applied to Errors.AR0109_Error.IRepository: the string "Repository" is not a valid pattern. It should contain a "*" or start with the "regex:" prefix.
error AR0109: AR0109_Error.cs(16,14): Error in the [NamingConvention] constraint applied to Errors.AR0109_Error.CustomerRepository: the string "Repository" is not a valid pattern. It should contain a "*" or start with the "regex:" prefix.
Resolution
Correct the pattern:
- Add a
*wherever the name can contain any text.*Repositorymatches every name that ends withRepository, includingRepositoryitself. - To write a regular expression, start the pattern with
regex:. The regular expression is not anchored: add^and$to match the whole name.
In the following example, the pattern is *Repository.
using System;
using System.Collections.Generic;
using PostSharp.Constraints;
namespace Errors.AR0109_Fixed;
public record Customer( int Id, string Name );
// The wildcard matches any name that ends with "Repository".
[NamingConvention( "*Repository" )]
public interface IRepository
{
Customer? Find( int id );
}
public class CustomerRepository : IRepository
{
private readonly Dictionary<int, Customer> customers = new() { [1] = new Customer( 1, "Contoso" ) };
public Customer? Find( int id ) => this.customers.GetValueOrDefault( id );
}
public static class Program
{
public static void Main()
{
Console.WriteLine( $"Customer 1: {new CustomerRepository().Find( 1 )?.Name}" );
}
}
Customer 1: Contoso
See Also
Reference
Other Resources