Open sandboxFocusImprove this doc

LA0068: Aspect Does Not Implement the Interface It Introduces

Severity Error
Reported by PostSharp
Message The aspect "<Aspect>" cannot introduce the interface "<Interface>" because the aspect type does not implement this interface.

Cause

When an aspect introduces an interface into its target type, with IntroduceInterfaceAttribute or with an IntroduceInterfaceAdviceInstance that an IAdviceProvider provides, the target type implements the interface by delegating each of its members to the aspect instance. The aspect class must therefore implement the interface itself. Otherwise, PostSharp reports LA0068 on the aspect class.

When <Interface> is a generic type definition, such as IComparable<>, PostSharp introduces every instance of this interface that the aspect class implements, and reports LA0068 when the aspect class implements none.

In the following example, the [Describable] aspect introduces IDescribable and has a Describe method, but the aspect class does not declare that it implements IDescribable.

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

namespace Errors.LA0068_Error;

public interface IDescribable
{
    string Describe();
}

// The aspect introduces IDescribable, but does not implement it.
[PSerializable]
[IntroduceInterface( typeof(IDescribable) )]
public class DescribableAttribute : InstanceLevelAspect
{
    public string Describe() => $"Instance of {this.Instance!.GetType().Name}";
}

[Describable]
public class Customer
{
}

public static class Program
{
    public static void Main()
    {
        IDescribable describable = (IDescribable) new Customer();
        Console.WriteLine( describable.Describe() );
    }
}
error LA0068: LA0068_Error.cs(16,14): The aspect "Errors.LA0068_Error.DescribableAttribute" cannot introduce the interface "Errors.LA0068_Error.IDescribable" because the aspect type does not implement this interface.

Resolution

Implement the introduced interface in the aspect class. When the aspect implements several interfaces, check that the type passed to IntroduceInterfaceAttribute is one of them.

In the following example, the aspect class implements IDescribable, so Customer implements it too.

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

namespace Errors.LA0068_Fixed;

public interface IDescribable
{
    string Describe();
}

// The aspect implements the interface that it introduces.
[PSerializable]
[IntroduceInterface( typeof(IDescribable) )]
public class DescribableAttribute : InstanceLevelAspect, IDescribable
{
    public string Describe() => $"Instance of {this.Instance!.GetType().Name}";
}

[Describable]
public class Customer
{
}

public static class Program
{
    public static void Main()
    {
        IDescribable describable = (IDescribable) new Customer();
        Console.WriteLine( describable.Describe() );
    }
}
Instance of Customer

See Also

Reference

IntroduceInterfaceAttribute
IntroduceInterfaceAdviceInstance

Other Resources

Introducing Interfaces, Methods, Properties and Events into Existing Classes