SharpCrafters Forum – Iterate over Laos attributes

SharpCrafters Support Forum

        


Iterate over Laos attributes Expand / Collapse
Author
Message
Posted 2/18/2010 9:18:26 PM


Jesper Niedermann

Community Member
Hi,

I have a problem where I need to iterate over the methods in an assembly and found out which methods are annotated by a Custom Laos Attribute. Not only directly annotated but also those methods that are annotated due to an annotation of the class or assembly.

I realize that when the IL is generated the Laos attributes are no longer .NET attributes and therefore I cannot use reflection.

But what should I do then ?

(What I need to do is to create a performance counter for each annotated method. And because of the way PerformanceCounters work I have to create them all in one swoop. Which means that I cannot simply create them during RuntimeInitialize. Therefore I naively thought that I should just make an installer that reflects over the assembly. But no such luck  <!-- s:? --> <!-- s:? -->  It must be possible somehow since you do it yourself when injecting the IL)


Hope you can help.
Post #2645
Posted 2/19/2010 12:11:30 PM


Gael Fraiteur

SharpCrafters
Hi,

Custom attributes are removed by default.
If you want to keep them, annotate your aspect class with:

[MulticastAttributeUsage(..., PersistMetaData=true)]

In your case, it may be better to use CompileTimeInitialize to build a list of all aspects, and use RuntimeInitialize to initialize all aspects.

Every aspect would refer to a list of all aspects, and only the first aspect to be initialized would initialize performance counters.

-gael
Post #2646
Posted 2/19/2010 11:03:46 PM


Jesper Niedermann

Community Member
Hi Gael,

Nice to communicate with you again. It has been 2-3 years.

I came up with exactly the same solution (using CompileTimeInitialize and RuntimeInitialize) and implemented it, and it works perfectly  <!-- s:) --> <!-- s:) -->  Must be the correct solution when 2 brilliant guys comes up with it independtly  <!-- s:D --> <!-- s:D -->

Thank you for the tip about PersistMetadata. Didn't know that existed. Might come in handy at a later time.

BTW: I read the stored methodnames from the Postsharp ressource in the assembly, by reading the entire ressource as as string, and parsing it. Is there at better interface to read the stored variables from the ressource ?

Regards Jesper
Post #2647
Posted 2/23/2010 12:31:11 PM


Gael Fraiteur

SharpCrafters
Hi Jesper,

Sorry for delay -- we have been busy with the web site migration.

I don't understand fully your last question; the normal way to store aspect-related data is in instance fields of aspects.

-gael
Post #3990
Posted 2/28/2010 11:25:10 AM


Jesper Niedermann

Community Member
Hi Gael,

I know that instance variables can be stored in the metadata, and that is what I have done. In my case I need to access metadata from other instances of the aspect. Kind of like a static variable I guess. So what I did was store the data from each instance of the aspect in a member variable in the CompileTimeInitialize. In my case it was the method name I stored. Then in the RuntimeInitialize I parsed the resource string to get all the method names.

It is a special case of course, but the way I did it seems rather crude, my guess would be that you provide a nicer, cleaner way of doing it. Like storing the data in a static variable instead. Perhaps that even works ? Have not tried it.

Regards Jesper
Post #4046
Posted 2/28/2010 12:26:15 PM


Gael Fraiteur

SharpCrafters
You could achieve this using a serializable singleton object containing the shared state, and having all aspects referring to this singleton as an instance field. It costs some memory (1 pointer per aspect instance) but would do the job quite nicely.

-g
Post #4047
Posted 2/28/2010 3:08:08 PM


Jesper Niedermann

Community Member
Ah. Of course. That would definitely be nicer. Thank you.
Post #4048
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:42am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.233. 7 queries. Compression Disabled.