Open sandboxFocusImprove this doc

LA0072: Introduced Method Has a Different Visibility than the Existing Method

Severity Error
Reported by PostSharp
Message Cannot introduce method "<Method>" into type "<TargetType>" because it is already defined in type "<DeclaringType>" with a visibility different from the Visibility property of the introduction.

Cause

When an aspect introduces a method with IntroduceMemberAttribute and sets its OverrideAction to OverrideOrFail or OverrideOrIgnore, PostSharp replaces or overrides the method of the same name and signature that already exists. The introduced method must have the same visibility as the existing one. When they differ, PostSharp reports LA0072, for both override actions.

The visibility of the introduced method is the Visibility property of the introduction, which is Public by default. The visibility of the method in the aspect class does not count.

<DeclaringType> is the type that defines the existing method: the target type itself, or one of its base types. For a property or an event, PostSharp checks each accessor, so <Method> is the name of the accessor, such as get_Name.

In the following example, the [Describable] aspect overrides the Describe method of Entity in Customer, but Entity.Describe is protected and the introduction is public.

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

namespace Errors.LA0072_Error;

[PSerializable]
public class DescribableAttribute : InstanceLevelAspect
{
    [IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrFail )]
    public string Describe() => $"Instance of {this.Instance!.GetType().Name}";
}

public class Entity
{
    // The method is protected, but the introduced method is public by default.
    protected virtual string Describe() => "Entity";

    public override string ToString() => this.Describe();
}

[Describable]
public class Customer : Entity
{
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Entity() );
        Console.WriteLine( new Customer() );
    }
}
error LA0072: LA0072_Error.cs(18,30): Cannot introduce method "Describe" into type "Errors.LA0072_Error.Customer" because it is already defined in type "Errors.LA0072_Error.Entity" with a visibility different from the Visibility property of the introduction.

Resolution

  • Set the Visibility property of the introduction to the visibility of the existing method, for example Family for a protected method.
  • Change the visibility of the existing method, when you own it.

In the following example, the introduction is protected, so the method introduced into Customer overrides Entity.Describe.

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

namespace Errors.LA0072_Fixed;

[PSerializable]
public class DescribableAttribute : InstanceLevelAspect
{
    // The introduced method is protected, like the method that it overrides.
    [IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrFail, Visibility = Visibility.Family )]
    public string Describe() => $"Instance of {this.Instance!.GetType().Name}";
}

public class Entity
{
    protected virtual string Describe() => "Entity";

    public override string ToString() => this.Describe();
}

[Describable]
public class Customer : Entity
{
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Entity() );
        Console.WriteLine( new Customer() );
    }
}
Entity
Instance of Customer

See Also

Reference

IntroduceMemberAttribute
MemberOverrideAction
Visibility

Other Resources

Introducing Interfaces, Methods, Properties and Events into Existing Classes