Open sandboxFocusImprove this doc

LA0133: Type-Level Aspect on the Module Type

Severity Error
Reported by PostSharp
Message The aspect "<Aspect>" cannot be applied to the "<Module>" special type.

Cause

Every module of .NET has a special type named <Module>, which holds the global fields and methods of the module and its module initializer. It is not a class of the program, so a type-level aspect, such as a TypeLevelAspect, cannot be applied to it.

Multicasting never selects this type, and GetTypes does not return it. PostSharp reports LA0133 when an aspect provider, an implementation of IAspectProvider, gets the <Module> type in another way, for example from its metadata token, and returns an AspectInstance for it. The message has no location: look for the aspect provider that adds the aspect named in the message.

In the following example, an assembly-level aspect provider adds the [Trace] aspect to the type of the first row of the type table of the module, which is <Module>.

using System;
using System.Collections.Generic;
using System.Reflection;
using Errors.LA0133_Error;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;

[assembly: TraceServices]

namespace Errors.LA0133_Error;

[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[PSerializable]
public class TraceServicesAttribute : AssemblyLevelAspect, IAspectProvider
{
    public IEnumerable<AspectInstance> ProvideAspects( object targetElement )
    {
        var assembly = (Assembly) targetElement;

        // The first row of the type table of a module is the <Module> special type, not a class of the program.
        Type type = Type.GetTypeFromHandle( assembly.ManifestModule.ModuleHandle.ResolveTypeHandle( 0x02000001 ) )!;

        yield return new AspectInstance( type, new TraceAttribute() );
    }
}

public class OrderService
{
    public void PlaceOrder()
    {
        Console.WriteLine( "Placing the order." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
error LA0133: The aspect "Errors.LA0133_Error.TraceAttribute" cannot be applied to the "<Module>" special type.

Resolution

Make the aspect provider return only the types of the program, for example the types that GetTypes returns, filtered by name or by attribute.

In the following example, the aspect provider adds the aspect to the types whose name ends with Service.

using System;
using System.Collections.Generic;
using System.Reflection;
using Errors.LA0133_Fixed;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;

[assembly: TraceServices]

namespace Errors.LA0133_Fixed;

[PSerializable]
public class TraceAttribute : TypeLevelAspect
{
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method, Attributes = MulticastAttributes.Public )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[PSerializable]
public class TraceServicesAttribute : AssemblyLevelAspect, IAspectProvider
{
    public IEnumerable<AspectInstance> ProvideAspects( object targetElement )
    {
        var assembly = (Assembly) targetElement;

        // GetTypes returns the types of the program, without the <Module> special type.
        foreach ( Type type in assembly.GetTypes() )
        {
            if ( type.Name.EndsWith( "Service", StringComparison.Ordinal ) )
            {
                yield return new AspectInstance( type, new TraceAttribute() );
            }
        }
    }
}

public class OrderService
{
    public void PlaceOrder()
    {
        Console.WriteLine( "Placing the order." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder();
    }
}
Entering PlaceOrder.
Placing the order.

See Also

Reference

IAspectProvider
AspectInstance
TypeLevelAspect

Other Resources

Adding Aspects Programmatically Using IAspectProvider