Metalama//Conceptual documentation/Creating aspects/Adding child aspects
Open sandboxFocus

Adding child aspects

An aspect can add other aspects to child declarations. Such aspects are called child aspects. Child aspects must satisfy two conditions:

  • The child aspect class must be processed after the parent aspect class; that is, the child aspect class must be positioned before the parent class in the AspectOrderAttribute ordering definition. See Ordering aspects for details.
  • The target declaration of the child aspect class must be contained in the target type of the parent aspect. For instance, a type-level aspect can add aspects to methods of the current type but not to methods of a different type. A parameter-level aspect can add a child aspect to the parent method or another method of the same type, but not of a different type.

An aspect can add child aspects from the BuildAspect method by using the builder.Outbound property, and then calling the AddAspect method. To add child aspects on members of the current declaration, use the Select or SelectMany method, and then call AddAspect.

Accessing the parent aspect

Parent aspects are listed in the IAspectPredecessor.Predecessors property, which the child aspect can access from meta.AspectInstance or builder.AspectInstance.