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?

Published 06 August 2003 07:15 AM by Ted_Graham

Comments

# Mahesh said on 06 August, 2003 09:24 AM
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
# Alex Hoffman said on 06 August, 2003 09:50 AM
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.
# Craig Andera said on 06 August, 2003 09:57 AM
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.
# Jesse Houwing said on 06 August, 2003 10:01 AM
XC# from www.resolvecomp.com does exactly that. It is just too bad that it's currently a little low on documentation.

It works as an add-in for Visual Studio that performs some post processing after an assembly has been compiled and adds the needed code at the beginning and the end of the methods.

I haven't used it with a webmethod specifically, but I think it should work.

One thing to koop in mind is that this method is not the standard Microsoft way of accomplishing this and that it may not work with future versions of th e.Net framework.
# Randy Ridge said on 06 August, 2003 10:09 AM
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...
# Randy Ridge said on 06 August, 2003 10:13 AM
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.
# hugo batista said on 01 June, 2004 08:19 PM
Hi

See RAIL at http://rail.dei.uc.pt . There is low documentation, and it's still a little unstable, but with a little work you can use it programmatically to do what you want.

i'm having the same problem than your friend and i'll use rail + nant to solve it.
# Matt Howells said on 10 February, 2008 09:38 AM

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

# ... said on 12 March, 2009 11:53 PM

Sehr wertvolle Informationen! Empfehlen!

Leave a Comment

(required) 
(required) 
(optional)
(required)