Sharing State with Advice
If you need to share compile-time state between different pieces of advice, or between your implementation of the BuildAspect
method and between advice, a few strategies are available to you.
Note
If you need to share run-time state with advice, you have to choose another strategy, for instance introducing a field in the target type and using it from several advice methods.
Warning
DO NOT share state with an aspect field if that state depends on the target declaration of the aspect. In case of inherited aspects, the same instance of the aspect class will be reused for all inherited targets.
Sharing state with compile-time template parameters
This is the most straightforward way to pass values from your BuildAspect
method to a template method, but it works only with method templates. For details, see Template Parameters and Type Parameters.
Sharing state with the Tags property
For event, properties or field templates, compile-time template parameters are not available. The simplest alternative is to use tags. Tags are arbitrary values assigned to arbitrary names.
To define and use tags:
In your implementation of the
BuildAspect
method, when adding the advice by calling a method of the IAdviceFactory interface, pass the tags as an anonymous object to thetags
argument like this:args: new { A = 5, B = "x", C = builder.Target.DeclaringType }
whereA
,B
andC
are three arbitrary names.In your template method, the tags are available under the
meta.Tags
dictionary. You would for instance use themeta.Tags["A"]
expression to access the tag namedA
that you defined in the previous step.
Example
Sharing state with the State property
You can use the IAspectBuilder.AspectState property to store any aspect state that depends on the target declaration. This object is exposed on the IAspectInstance.AspectState property and is therefore also visible to inheritors and children aspects.