Open sandboxFocusImprove this doc

LA0070: Introduced Method Conflicts with a Static Method

Severity Error
Reported by PostSharp
Message Cannot introduce method "<Method>" into type "<TargetType>" because it is already defined in type "<DeclaringType>" as a static method.

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. A static method can be neither replaced nor overridden by an introduced method, so PostSharp reports LA0070, for both override actions.

<DeclaringType> is the type that defines the static 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 introduces a Describe instance method into Customer, whose base class Entity has a static Describe method.

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

namespace Errors.LA0070_Error;

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

public class Entity
{
    // The static method has the name and signature of the introduced instance method.
    public static string Describe() => "Entity";
}

[Describable]
public class Customer : Entity
{
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( Entity.Describe() );

        // The method introduced into Customer is not visible at compile time, so it is called with the dynamic keyword.
        dynamic customer = new Customer();
        Console.WriteLine( customer.Describe() );
    }
}
error LA0070: LA0070_Error.cs(18,26): Cannot introduce method "Describe" into type "Errors.LA0070_Error.Customer" because it is already defined in type "Errors.LA0070_Error.Entity" as a static method.

Resolution

  • Rename the static method or the introduced method.
  • Set the OverrideAction of the introduction to Ignore to skip the introduction whenever the method exists.

In the following example, the static method of Entity is renamed DescribeKind, so the aspect introduces Describe into Customer.

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

namespace Errors.LA0070_Fixed;

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

public class Entity
{
    // The static method has another name, so it no longer conflicts with the introduced method.
    public static string DescribeKind() => "Entity";
}

[Describable]
public class Customer : Entity
{
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( Entity.DescribeKind() );

        // The method introduced into Customer is not visible at compile time, so it is called with the dynamic keyword.
        dynamic customer = new Customer();
        Console.WriteLine( customer.Describe() );
    }
}
Entity
Instance of Customer

See Also

Reference

IntroduceMemberAttribute
MemberOverrideAction

Other Resources

Introducing Interfaces, Methods, Properties and Events into Existing Classes