Open sandboxFocusImprove this doc

COM018: Relationship Attribute on the Backing Field of a Value Property

Severity Warning
Reported by PostSharp.Patterns.Aggregation
Message The [<Attribute>] attribute applied to the backing field of the automatic property <Property> is ignored because the type of the property is a string, a delegate, or a value type.

Cause

Strings, delegates and value types such as int or DateTime are values: they are never children, parents or references of an object model. A relationship attribute, ChildAttribute, ParentAttribute or ReferenceAttribute, on a member of such a type is an error, COM004, when it is on the property. When the field: target applies it to the backing field of an automatic property, and the property has no relationship attribute, PostSharp ignores it and reports the warning COM018 instead.

In the following example, the Name string property of an [Aggregatable] class has [Reference] on its backing field.

using System;
using PostSharp.Patterns.Model;

namespace Errors.COM018_Error;

[Aggregatable]
public class Customer
{
    // A string is never a child, a parent or a reference of the object model.
    [field: Reference] public string Name { get; set; } = "Alice";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( $"Customer: {new Customer().Name}" );
    }
}
warning COM018: COM018_Error.cs(10,38): The [Reference] attribute applied to the backing field of the automatic property Errors.COM018_Error.Customer.Name is ignored because the type of the property is a string, a delegate, or a value type.

Resolution

Remove the attribute. A property of a string, delegate or value type needs no relationship attribute.

using System;
using PostSharp.Patterns.Model;

namespace Errors.COM018_Fixed;

[Aggregatable]
public class Customer
{
    // A string property needs no relationship attribute.
    public string Name { get; set; } = "Alice";
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( $"Customer: {new Customer().Name}" );
    }
}
Customer: Alice

See Also

Reference

AggregatableAttribute
ReferenceAttribute

Other Resources

Annotating an Object Model for Parent/Child Relationships (Aggregatable)