Feature Focus: Showing message locations in the source code

by Igal Tabachnik on 04 Jan 2012

During compilation, PostSharp takes great care in making sure that everything works correctly. When something goes wrong, PostSharp will report it as an error or a warning. Until now, however, whenever an error or a warning occurred, the developer had to manually navigate to that place in code.

We are excited to announce that with PostSharp 2.1 we’ve enhanced the errors and warnings with the exact location information, allowing you to simply double-click on the message and you’ll be taken to the error line!

To enable this feature, go to Tools – Options – PostSharp, and under Experimental, set Resolve Message Location to True.

Then, simply rebuild your solution, and if there are any warnings or errors, you’ll be able to see exactly where they are.

This is accomplished by specifying the member (in this case, the method) that is responsible for the message, in the aspect’s CompieTimeValidate method:

// Validate the attribute usage.
public override bool CompileTimeValidate( MethodBase method )
{
    // Don't apply to constructors.
    if ( method is ConstructorInfo )
    {
        Message.Write( method, SeverityType.Error, "CX0001", 
                       "Cannot cache constructors." );
        return false;
    }

    MethodInfo methodInfo = (MethodInfo) method;

    // Don't apply to void methods.
    if ( methodInfo.ReturnType.Name == "Void" )
    {
        Message.Write( method, SeverityType.Error, "CX0002", 
                       "Cannot cache void methods." );
        return false;
    }

    // Does not support out parameters.
    ParameterInfo[] parameters = method.GetParameters();
    for ( int i = 0; i < parameters.Length; i++ )
    {
        if ( parameters[i].IsOut )
        {
            Message.Write( method, SeverityType.Error, "CX0003", 
                           "Cannot cache methods with return values." );
            return false;
        }
    }

    return true;
}

Aspect developers are encouraged to include the member in error/warning messages. For more information, please refer to the documentation on Working with Errors, Warnings and Messages.

Please note that this is not enabled by default as it is still experimental and might have an impact on performance. Please let us know how it works out for you!

Happy PostSharping!

-Igal