Open sandboxFocusImprove this doc

COM012: Relationship Attribute on a Static Member

Severity Warning
Reported by PostSharp.Patterns.Aggregation
Message Attribute [<Attribute>] cannot be applied to "<Member>" because it is a static member; it will be ignored.

Cause

The ChildAttribute, ReferenceAttribute and ParentAttribute attributes describe how the objects of an AggregatableAttribute class relate to each other: a child belongs to one parent instance. A static field or property belongs to no instance, so it cannot take part in the parent/child graph. When one of these attributes is applied to a static field or a static automatic property, PostSharp reports COM012 and ignores the attribute: the object stored in the member gets no parent.

<Attribute> is the name of the attribute class, for example ChildAttribute.

In the following example, the static ShippingAddress property is marked as a child, so the address does not get the customer as its parent.

using System;
using PostSharp.Patterns.Model;

namespace Errors.COM012_Error;

[Aggregatable]
public class Customer
{
    public string? Name { get; set; }

    // A static property cannot be a child of an instance.
    [Child]
    public static Address? ShippingAddress { get; set; }
}

[Aggregatable]
public class Address
{
    [Parent]
    public Customer? Customer { get; private set; }

    public string? Line1 { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var customer = new Customer { Name = "Joe Johnson" };
        Customer.ShippingAddress = new Address { Line1 = "1 Main Street" };

        Console.WriteLine( $"Parent is the customer: {Customer.ShippingAddress.Customer == customer}" );
    }
}
warning COM012: COM012_Error.cs(13,28): Attribute [ChildAttribute] cannot be applied to "Errors.COM012_Error.Customer.ShippingAddress" because it is a static member; it will be ignored.
Parent is the customer: False

Resolution

  • When each instance owns its own object, make the field or property an instance member.
  • When the object is shared by all instances, remove the attribute from the static member.

In the following example, ShippingAddress is an instance property, so the address gets the customer as its parent.

using System;
using PostSharp.Patterns.Model;

namespace Errors.COM012_Fixed;

[Aggregatable]
public class Customer
{
    public string? Name { get; set; }

    // An instance property makes the address a child of each customer.
    [Child]
    public Address? ShippingAddress { get; set; }
}

[Aggregatable]
public class Address
{
    [Parent]
    public Customer? Customer { get; private set; }

    public string? Line1 { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var customer = new Customer { Name = "Joe Johnson" };
        customer.ShippingAddress = new Address { Line1 = "1 Main Street" };

        Console.WriteLine( $"Parent is the customer: {customer.ShippingAddress.Customer == customer}" );
    }
}
Parent is the customer: True

See Also

Reference

AggregatableAttribute
ChildAttribute
ReferenceAttribute
ParentAttribute

Other Resources

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