5

I'm implementing a PostSharp aspect library and can't find out a solution to the following problem.

Suppose we have an aspect that will be applied for some methods and won't be applied for the others. I need some mechanism that I can use at runtime to know whether an aspect was applied to a method. Specifically, what is the recommended way to determine at runtime whether certain aspect was applied to a particular method given by a System.Reflection.MethodBase?

The first solution that comes into my head is to make PostSharp mark every method that has been modified by this aspect with a custom atribute and use methodBase.CustomAttributes at runtime. Is this the right solution for the problem? Or maybe there already is a ready-to-use or more elegant solution in PostSharp.

Also, please take into account that preferred is a solution that doesn't require a paid PostSharp license since Free Community Edition is enough for my current project. Anyway, it would be very intresting to discuss solutions based on paid PostSharp functionality too.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
  • what kind of aspects are we talking about? If you inherit for example from methodInterceptionAttribute then you can add the [MyAttribute] to the method and it would be applied to just the methods you specify. would this be sufficient? – Batavia Dec 30 '14 at 15:06
  • postsharp applies attributes compile time. why would you need to determine at runtime if a attribute has been applied. – Batavia Dec 30 '14 at 15:11
  • @Batavia, My current situation is about the `OnExceptionAspect`, but I'm wondering how to implement this regerdless of a specific aspect type. There is `PostSharp.Post.IsTransformed` to determine at runtime If the assembly has been transformed. I'm searching something like this but for single methods. – Alexander Abakumov Dec 30 '14 at 16:45
  • @Batavia, However, could you please point to a solution to mark a method with an attribute using `MethodInterceptionAspect` specifically? All I could find is how to inject attributes using IAspectProvider, for example [this](http://stackoverflow.com/a/7852177/3345644). Additionally, this approach requires paid Composite Attribute feature of PostSharp. – Alexander Abakumov Dec 30 '14 at 16:50
  • @Batavia, Answering to your second comment, one of the use cases of the aspects library I'm implementing is that when an exception occures, I need to log full stack trace with parameter values of every method that was transformed by my aspect skipping other methods in the stack trace. – Alexander Abakumov Dec 30 '14 at 16:52

1 Answers1

6

When you're adding a method-level aspect (e.g. OnExceptionAspect) as an attribute on the class or the assembly level, PostSharp multicasts this attribute to all the applicable methods in that class/assembly. At the next stage the aspect transforms each method with the attribute and the attribute itself is removed.

You can tell PostSharp to keep all the aspect attributes by setting the MulticastAttributeUsageAttribute.PersistMetaData property to true. The [MulticastAttributeUsage] attribute must be applied to your aspect class.

[MulticastAttributeUsage( PersistMetaData = true )]
public class MyAspect : OnExceptionAspect
{
    // ...
}

Now you can look for MyAspect in MethodBase.CustomAttributes to determine whether the aspect has been applied to the current method.

AlexD
  • 5,011
  • 2
  • 23
  • 34