Overview
PostSharp Laos provides a set of aspects like
OnMethodBoundaryAspect,
OnFieldAccessAspect and
CompositionAspect. Although these aspects cover most
frequent needs, they cannot fit all scenarios.
If you have special requirements, you can implement new aspect kinds using the PostSharp Laos.
Since PostSharp Laos extensions are standard plug-ins of PostSharp Core, we suggest you read first the documentation of PostSharp Core for more information about using low-level APIes of PostSharp, and especially chapter Developing Plug-Ins for PostSharp.
A new aspect kind is composed of two parts:
- The public interface is typically a custom
attribute derived from LaosFieldLevelAspect,
LaosMethodLevelAspect,
or LaosTypeLevelAspect. The
assembly containing the public interface of your new aspects
typically refers to
PostSharp.PublicandPostSharp.Core. - The implementation uses PostSharp Core low-level APIes to perform MSIL transformations required by the new aspect.
Designing the public interface
Using multicast attributes
If you want your aspect to be usable as a multicast custom attribute, you can derive it from one of the classes LaosFieldLevelAspect, LaosMethodLevelAspect, or LaosTypeLevelAspect. You have to override the GetPostSharpRequirements method and insert a dependency to your implementation. Additionally, all aspects should be serializable.
[Serializable]
public class MyAspect : LaosMethodLevelAspect
{
public override PostSharpRequirements GetPostSharpRequirements()
{
PostSharpRequirements requirements = base.GetPostSharpRequirements();
requirements.PlugIns.Add( "MyExtensionPlugIn" );
requirements.Tasks.Add("MyExtensionTask");
return requirements;
}
}
Aspect without multicasting semantics
Alternatively, if you don't want multicasting semantics, you can
derive a class from System.Attribute make it implement
one of the interfaces ILaosFieldLevelAspect,
ILaosMethodLevelAspect,
or ILaosTypeLevelAspect.
Additionally, your aspect should implement the interface IRequirePostSharp.
While implementing the GetPostSharpRequirements
method, you should not forget to import the requirements of
PostSharp Laos:
[Serializable]
public class MyAspect : Attribute, ILaosMethodLevelAspect
{
public PostSharpRequirements GetPostSharpRequirements()
{
PostSharpRequirements requirements = LaosRequirements.GetRequirements();
requirements.PlugIns.Add( "MyExtensionPlugIn" );
requirements.Tasks.Add("MyExtensionTask");
return requirements;
}
}
If you don't plan your aspect to be used as a custom attribute,
you don't event have to derive your aspect from the class
System.Attribute. However, the
CompoundAspect adding your sub-aspect should override
the GetPostSharpRequirements method to provide a
dependency to your implementation.
Note. Instead of specifying the dependency of
your aspect to PostSharp Laos in the
GetPostSharpRequirements method, you can do it in the
task type configuration, in the plug-in configuration
(.psplugin) file.
Designing the implementation
A PostSharp Laos extension has the form of a PostSharp Core
plug-in. You need a single task implementing the ILaosAspectWeaverFactory
interface. The name of this task and the name of the plug-in are
typically added to the list of requirements in the
GetPostSharpRequirements method of the public
interface.
The ILaosAspectWeaverFactory interface has a single
method CreateAspectWeaver accepting as parameter an
instance of the aspect (i.e. of the public interface). This method
should return an instance of the weaver for this aspect, or
null if the current plug-in does not know this aspect.
At build time, this method is invoked for every aspect and for
every registered weaver factory, until a factory provides a
weaver.
The weaver should be derive from the LaosAspectWeaver class (or
one of MethodLevelAspectWeaver,
FieldLevelAspectWeaver,
or TypeLevelAspectWeaver).
You need to implement aspect validation, runtime aspect
initialization, and of course aspect implementation.