Open sandboxFocusImprove this doc

PS0093: Invalid Regular Expression in a Multicast Filter

Severity Error
Reported by PostSharp
Message Invalid expression for property <Property> of the custom attribute "<AttributeType>" on <DeclarationKind> "<Declaration>": <Error>

Cause

The properties of MulticastAttribute that filter targets by name, AttributeTargetTypes, AttributeTargetMembers, AttributeTargetParameters and AttributeTargetAssemblies, as well as the MemberName property of a pointcut, accept a name, a pattern with the * wildcard, or a regular expression when the value starts with the regex: prefix. PostSharp reports PS0093 when the text after the regex: prefix is not a valid .NET regular expression.

<Property> is the property that contains the expression, <Declaration> is the declaration that the attribute is applied to, and <Error> is the reason given by the .NET regular expression parser, including the position of the problem in the expression. When the expression is in a multicast attribute, PostSharp then stops the build with PS0055, because it does not continue after an error in the multicast attributes.

In the following example, the regular expression of AttributeTargetMembers lacks its closing parenthesis.

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0093_Error;

[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

// The regular expression lacks its closing parenthesis.
[Trace( AttributeTargetMembers = "regex:^(Place|Cancel" )]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Order placed." );

    public void CancelOrder() => Console.WriteLine( "Order canceled." );
}

public static class Program
{
    public static void Main()
    {
        OrderService service = new();
        service.PlaceOrder();
        service.CancelOrder();
    }
}
error PS0093: PS0093_Error.cs(18,14): Invalid expression for property AttributeTargetMembers of the custom attribute "Errors.PS0093_Error.TraceAttribute" on typedef "Errors.PS0093_Error.OrderService": Invalid pattern '^(Place|Cancel' at offset 14. Not enough )'s.
error PS0055: The processing of multicast attributes cannot continue because of the previous errors.

Resolution

Correct the regular expression, as <Error> describes. In C#, a backslash in a regular expression must be doubled in a regular string literal, or written in a verbatim string literal (@"regex:..."). When you do not need a regular expression, use a name with the * wildcard and without the regex: prefix.

In the following example, the parentheses of the regular expression are balanced.

using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0093_Fixed;

[PSerializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

// The parentheses of the regular expression are balanced.
[Trace( AttributeTargetMembers = "regex:^(Place|Cancel)" )]
public class OrderService
{
    public void PlaceOrder() => Console.WriteLine( "Order placed." );

    public void CancelOrder() => Console.WriteLine( "Order canceled." );
}

public static class Program
{
    public static void Main()
    {
        OrderService service = new();
        service.PlaceOrder();
        service.CancelOrder();
    }
}
Entering PlaceOrder.
Order placed.
Entering CancelOrder.
Order canceled.

See Also

Reference

AttributeTargetTypes
AttributeTargetMembers

Other Resources

Adding Aspects to Multiple Declarations Using Attributes
Understanding Attribute Multicasting