| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot introduce property "<Property>" into type "<TargetType>" because a property with the same name and parameters, but of a different type, is defined in type "<DeclaringType>". |
Cause
When an aspect introduces a property with IntroduceMemberAttribute and sets its OverrideAction to OverrideOrFail, PostSharp replaces or overrides the property that the target type already defines. The existing property is the one with the same name and the same index parameters. When the type of this property differs from the type of the introduced property, the introduced property cannot replace it, and PostSharp reports LA0063.
<DeclaringType> is the type that defines the existing property: the target type itself, or one of its base types.
In the following example, the [Versioned] aspect introduces a Version property of type int into Document, which already defines a Version property of type string.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0063_Error;
[PSerializable]
public class VersionedAttribute : InstanceLevelAspect
{
[IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrFail )]
public int Version { get; set; }
}
[Versioned]
public class Document
{
// The aspect introduces a property with the same name, but of type int.
public string Version { get; set; } = "1.2";
}
[Versioned]
public class Report
{
}
public static class Program
{
public static void Main()
{
Console.WriteLine( $"Document.Version: {typeof(Document).GetProperty( "Version" )?.PropertyType.Name}" );
Console.WriteLine( $"Report.Version: {typeof(Report).GetProperty( "Version" )?.PropertyType.Name}" );
}
}
error LA0063: LA0063_Error.cs(19,19): Cannot introduce property "Version" into type "Errors.LA0063_Error.Document" because a property with the same name and parameters, but of a different type, is defined in type "Errors.LA0063_Error.Document".
Resolution
- Give the introduced property and the existing property the same type.
- Set the OverrideAction of the introduction to OverrideOrIgnore: PostSharp then skips the introduction when the existing property has another type, and still overrides a property of the same type. Ignore skips the introduction whenever the property exists.
- Otherwise, rename the existing property or the introduced one.
In the following example, the introduction is skipped in Document, which keeps its string property, and happens in Report.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0063_Fixed;
[PSerializable]
public class VersionedAttribute : InstanceLevelAspect
{
// The introduction is skipped in a class that defines the property with another type.
[IntroduceMember( OverrideAction = MemberOverrideAction.OverrideOrIgnore )]
public int Version { get; set; }
}
[Versioned]
public class Document
{
public string Version { get; set; } = "1.2";
}
[Versioned]
public class Report
{
}
public static class Program
{
public static void Main()
{
Console.WriteLine( $"Document.Version: {typeof(Document).GetProperty( "Version" )?.PropertyType.Name}" );
Console.WriteLine( $"Report.Version: {typeof(Report).GetProperty( "Version" )?.PropertyType.Name}" );
}
}
Document.Version: String
Report.Version: Int32
See Also
Reference
IntroduceMemberAttribute
MemberOverrideAction
Other Resources
Introducing Interfaces, Methods, Properties and Events into Existing Classes