Using custom attributes to add code to a method?
A friend of mine wrote yesterday to ask about having custom attributes that insert code in the decorated method. He has a group of web methods, some of which need to be secured with custom authentication. He wrote, “I'd like to get fancy and just have anyone needing the custom authentication apply an attribute to their web method.”
I just reread the section in Richter about custom attributes, and I don't see a way to do this. His discussion of affecting program behavior through attributes assumes that the class decorated with the attribute will be passed to another method, which will detect the presence of the attribute and do something special. The only way that would work for the above scenario is if all the web method calls came through a central method that checked security if the attribute was present and then delegated to the right web method. And that is too ugly to consider.
What we need is a way to insert code in a method by decorating it with a custom attribute. Anyone solved this?