PostSharpAPI ReferencePost­Sharp.​AspectsComposition­Aspect
Open sandboxFocus

CompositionAspect Class

Aspect that, when applied on a type, introduces one or many new interfaces into that type.

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
public abstract class CompositionAspect : TypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICompositionAspect, ITypeLevelAspect, IAspect


Consider using a composite aspect and introducing an interface using IntroduceMemberAttribute instead of using a CompositionAspect.

The GetPublicInterfaces(Type) method is invoked at build time. At runtime, the method CreateImplementationObject(AdviceArgs) should return an object implementing all interfaces.

Use the Post.Cast<TSource, TTarget>(TSource) method to cast the enhanced type to the newly implemented interface. This cast is verified during post-compilation.

Properties OverrideAction and AncestorOverrideAction determine what should happen if the target type already implements the interface directly or indirectly.

By default, the object implementing the interface is stored as a serializable field. If the property NonSerializedImplementation is set to true, this field will be marked as NonSerializedAttribute.


All classes implementing IAspect should typically be marked as serializable using the SerializableAttribute or PSerializableAttribute custom attribute . Fields that are only used at runtime (and unknown at compile-time) should be carefully marked with the NonSerializedAttribute or PNonSerializedAttribute custom attribute. When PostSharp is used on a platform that does not support aspect serialization (such as .NET Compact Framework, Silverlight, or Windows Phone), or when another aspect serializer is used, it is not necessary to mark the aspect class as serializable. For more information, see Understanding Aspect Serialization .

Understanding Aspect Serialization


Name Description


Name Description

Specifies the action (Fail or Ignore) to be overtaken when an ancestor of one of the interfaces returned by GetPublicInterfaces(Type) is already implemented by the type to which the current aspect is applied.


This property has no effect.


Determines whether the field containing the interface implementation (and storing the object returned by CreateImplementationObject(AdviceArgs)) should be excluded from serialization by BinaryFormatter. The same effect is typically obtained by applying the NonSerializedAttribute custom attribute to the field.


Specifies the action (Fail or Ignore) to be overtaken when one of the interfaces returned by the GetPublicInterfaces(Type) method is already implemented by the type to which the current aspect is applied.


Name Description

Method invoked at build time to create a concrete AspectConfiguration instance specifically for the current Aspect type.


Method invoked at runtime, during the initialization of instances of the target type, to create the composed object.


Gets the array of interfaces that should be introduced publicly into the target type of the current aspect.

SetAspectConfiguration(AspectConfiguration, Type)

Method invoked at build time to set up an AspectConfiguration object according to the current Aspect instance and a specified target element of the current aspect.

See Also