Aspect that, when applied on a type, introduces one or more new interfaces into that type.
Implements
Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
[MulticastAttributeUsage(MulticastTargets.Class|MulticastTargets.Struct, AllowMultiple = true)]
[AspectConfigurationAttributeType(typeof(CompositionAspectConfigurationAttribute))]
[Serializer(null)]
public abstract class CompositionAspect : TypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICompositionAspect, ITypeLevelAspect, IAspectRemarks
note
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.
The 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.
note
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 another aspect serializer (such as MsilAspectSerializer) is used, it is not necessary to mark the aspect class as serializable. For more information, see Understanding Aspect Serialization.
Constructors
| Name | Description |
|---|---|
| CompositionAspect() |
Properties
| Name | Description |
|---|---|
| AncestorOverrideAction | Gets or sets the action (Fail or Ignore) to take 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. |
| GenerateImplementationAccessor | This property has no effect. |
| NonSerializedImplementation | Gets or sets a value indicating whether the field containing the interface implementation should be excluded from serialization by BinaryFormatter. This field stores the object returned by CreateImplementationObject(AdviceArgs). The same effect is typically obtained by applying the NonSerializedAttribute custom attribute to the field. |
| OverrideAction | Gets or sets the action (Fail or Ignore) to take when one of the interfaces returned by the GetPublicInterfaces(Type) method is already implemented by the type to which the current aspect is applied. |
Methods
| Name | Description |
|---|---|
| CreateAspectConfiguration() | Method invoked at build time to create a concrete AspectConfiguration instance specifically for the current Aspect type. |
| CreateImplementationObject(AdviceArgs) | Method invoked at runtime, during the initialization of instances of the target type, to create the composed object. |
| GetPublicInterfaces(Type) | 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. |