ohnezahn
ohnezahn

Reputation: 483

Postsharp: Access already applied aspects from an Aspect

I have created a small hierarchy of Aspects. Example:

public abstract class MyBaseAspectAttribute : Aspect, IAspectProvider
{
...
    public IEnumerable<AspectInstance> ProvideAspects( object targetElement )
    {
    }
...
}

I was wondering if there is any way, inside the ProvideAspects, to find out if and what other Aspects are already applied to the targetElement - and get a reference to the Aspect instance.

Something like

PostShart.GetAspects(targetElement).OfType<MyBaseAspectAttribute>();

(my intention: check if a certain aspect is already added and if yes, then modify it)

PS: I did solve my problem in another way, but I if this would be possible it would make for a more elegant and efficient solution than mine.

Upvotes: 2

Views: 46

Answers (1)

Gael Fraiteur
Gael Fraiteur

Reputation: 6857

You can use the aspect repository:

http://www.postsharp.net/blog/post/New-in-PostSharp-40-Aspect-Repository http://doc.postsharp.net/t_postsharp_aspects_iaspectrepositoryservice

If you want to modify the aspect instance, you need to store the instances in a dictionary stored in a static field. The dictionary can be populated from the aspect constructor so it not only covers the case of IAspectProvider, but also when the aspect is applied as a custom attribute.

Upvotes: 2

Related Questions