Open sandboxFocusImprove this doc

LA0192: Introduced Method Differs from the Existing Method by an In Modifier

Severity Error
Reported by PostSharp
Message Cannot introduce method "<Method>" into type "<TargetType>" because it is already defined in type "<DeclaringType>" with a parameter that is 'in' in one method and 'ref' or 'out' in the other.

Cause

An aspect introduces a method into its target type with IntroduceMemberAttribute, and the OverrideAction of the introduction lets it replace or override an existing method, for example OverrideOrFail.

In the compiled code, in, ref and out parameters have the same type, a reference to the value, so PostSharp finds the existing method although the C# declarations differ. An in parameter is read-only, while a ref or out parameter can be written to, so PostSharp does not let one method replace the other. It reports LA0192 when a parameter is in in one of the two methods and ref or out in the other.

<DeclaringType> is the type that defines the existing method: the target type itself, or one of its base types. The message has no location in the source code.

In the following example, the [Audited] aspect introduces Record( ref decimal ) into Ledger, which already defines Record( in decimal ).

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

namespace Errors.LA0192_Error;

[PSerializable]
public class AuditedAttribute : InstanceLevelAspect
{
    // The introduced method takes the parameter as ref, but the existing method takes it as in.
    [IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrFail )]
    public void Record( ref decimal amount )
    {
        Console.WriteLine( $"Audited entry: {amount}" );
    }
}

[Audited]
public class Ledger
{
    public void Record( in decimal amount )
    {
        Console.WriteLine( $"Entry: {amount}" );
    }
}

public static class Program
{
    public static void Main()
    {
        new Ledger().Record( 100m );
    }
}
error LA0192: LA0192_Error.cs(22,17): Cannot introduce method "Record" into type "Errors.LA0192_Error.Ledger" because it is already defined in type "Errors.LA0192_Error.Ledger" with a parameter that is 'in' in one method and 'ref' or 'out' in the other.

Resolution

Give each parameter the same modifier in the introduced method and in the existing method: in in both, or ref in both. Otherwise, rename one of the two methods, or set OverrideAction to Ignore to keep the existing method.

In the following example, the introduced method takes the parameter as in, and replaces the existing method.

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

namespace Errors.LA0192_Fixed;

[PSerializable]
public class AuditedAttribute : InstanceLevelAspect
{
    // The introduced method takes the parameter as in, like the existing method.
    [IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrFail )]
    public void Record( in decimal amount )
    {
        Console.WriteLine( $"Audited entry: {amount}" );
    }
}

[Audited]
public class Ledger
{
    public void Record( in decimal amount )
    {
        Console.WriteLine( $"Entry: {amount}" );
    }
}

public static class Program
{
    public static void Main()
    {
        new Ledger().Record( 100m );
    }
}
Audited entry: 100

See Also

Reference

IntroduceMemberAttribute
MemberOverrideAction

Other Resources

Introducing Interfaces, Methods, Properties and Events into Existing Classes