1
votes

I have an aspect, the usual Cache built using Postsharp. The problem I have is that when PostSharp weaves the code, the result code will not have the attribute on the method anymore, which makes sense for a few reasons:

  1. It is not anymore useful, as it was intended for PostSharp
  2. It can be applied at assembly level and there is no [CacheAttribute] on affected methods to start with

But.. I am trying to do some unit testing and I would like to see the attribute on the resulted code so I could say: "Check if the method x has a cache attribute on it that says it should cache for 50 seconds". And that would be all my unit test, rather than mocking the cache manager every time. The aspect is applied on as many methods I want and I can test the aspect separately and only check if cache is applied by checking the attribute.

So, how can I say to Postsharp to weave an attribute on the affected methods, that is identical with the CacheAttribute that is using to weave the code in the first place? Ok, it can be other attribute so they don't conflict, but must have the properties of the CacheAttribute.

http://www.postsharp.net/blog/post/PostSharp-Principals-Day-12-e28093-Aspect-Providers-e28093-Part-1 - it tells me how to add attributes on fields and properties, but not on methods.

Cheers.

1

1 Answers

0
votes

I found the solution, there is no need to add a new attribute myself. Postsharp solves this by letting you add PersistMetadata - true on the multicast attribute:

[MulticastAttributeUsage(PersistMetaData = true)]
public class CacheAttribute : MethodInterceptionAspect
{...}

Works magically.