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?

6 Comments

  • Your friend might either have to write a custom proxy or derive from ContextBoundObject and use interception to do the extra task/code that he needs executing for methods decorated with an attribute.



    You should be able to get some sample code on MSDN for the above two methods.

    Cheers

    Mahesh

  • Ted, take a look at the article on using contexts to facilitate component services in the March 2003 issue of MSDN mag. It describes how to implement custom context attributes. I believe that this is the mechanism that Microsoft will use to provide end user support for web service extensions in the future.

  • Another option is to write a SoapExtension that examines the target method for the attribute and performs the appropriate pre- or post-processing. It's WebMethod-specific, but probably a little bit easier to deal with than custom proxies or more esoteric methods of code injection.



    Honestly, IMO, the best way to inject code into a method is to add an explicit call to some other method. If you're adding an attribute, you're already changing source code anyway, and this approach has the advantage that what's going on is totally obvious.

  • I've done this to solve an odd problem that I've had. I needed a way to call a method using only a function pointer wherein you don't know the dll at compile time. Also, I use this method to set a cdecl calling convention on a delegate. Both of these things can be done using MSIL, so I came up with this hackish solution. For the MSIL injection, I have a custom attribute called IlasmAttribute, it looks like so when in use:

    <br><br>

    [IlasmAttribute(".maxstack 2\r\nldarg foo\r\nldarg extensionPointer\r\ncalli unmanaged stdcall void(float32)\r\nret")]<br>

    public static void glFogCoordfEXT(IntPtr extensionPointer, Single foo) {}<br><br>



    Hopefully that apprears vaguley correct in the comments. Then on the post-build event, I've got a batch file that runs which ildasm's my assembly, runs a post-process app which does regex replacements of the attribute call and injects the supplied MSIL in the appropriate place (also it does the appropriate replacements to inject the cdecl calling convention on similarly tagged delegates as mentioned before). Then the modified MSIL is ilasm'ed back. It works fine for me, but it's quite the kludge...

  • I guess I should reiterate that this is done on a library at compile time for a very specific purpose. The end-user never uses or even sees this attribute, so upon rereading your post, I doubt it would be of much use to your friend. My bad.

  • This sounds like Aspect Oriented Programming. Take a look at Spring.NET.

Comments have been disabled for this content.