views:

160

answers:

2

Hey,

I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives.

I would use reflection, but the MethodInfo class does not reference the point in the original source.

EDIT: What I am really trying to do here is to support pre-conditions on methods, that pre-condition code being determined by attributes on the method. My initial thought being that I could look for the beginning of the method, and then insert generated code for handling the pre-conditions.

Is there a better way to do this? I am open to creating a Visual Studio Addin if need be.

This is a .NET 2.0 project.

Cheers

+3  A: 

PostSharp or Mono.Cecil will let you do this cleanly by altering the generated code without getting into writing a C# parser which is unlikely to be core business for you...

Havent done anything of consequence with PostSharp but would be guessing its more appropriate than Mono for implementing something like preconditions or AOP. Alternately you might be able to do something AOPy with a DI container like Ninject

But of course the applicability of this idea Depends - you didnt say much other than that you wanted to insert code at the start of methods...

EDIT: In light of your desire to do preconditions... Code Contracts in .net 4 is definitely in that direction.

What sort of a tool do you have? Whats wrong with having a single Mono.Cecil.dll DLL shipped? Either way something other than a parser is the tool for the job.

Ruben Bartelink
I've taken a swift glance at PostSharp - looks good, but I'd prefer not to ship a 3rd party library with the product in order for this to work...
Kazar
If you don't want to ship a 3rd party library, use ILMerge to merge those assemblies into yours. You can use a post-build step to do this.
Joel Lucsy
I'm assuming however, that PostSharp is going to need to be installed for every developer that needs to build using these pre-conditions?
Kazar
Does Code Contracts not suit? If you're going to post-process code, you'll need to have something running *after* each build (but it could be as simple as having something referenced in your build scripts). Go look at some walkthroughs of how you can integrate PostSharp into a build. If you want codegen based on attributes, its not free, but you were willing to consider parsing C# so you must be willing to take some pain :D
Ruben Bartelink
In light of .NET 2.0 restrictions you mention, I suggest you go look into PostSharp including a video of how you integrate it into your build script.
Ruben Bartelink
Ok, so I have PostSharp working - I had a project in which I put the Precondition attribute, hoping to be able to reference that project in my other solution projects - however, every project that uses that PreCondition also has to reference PostSharp, which is annoying. Also, ILMerge prevents PostSharp from working.
Kazar
Oh, and this is marked as the answer because it was very helpful! Thanks Ruben.
Kazar
@Kazar: Glad to help. Dont know enough about speciifics of PostSharp to help with the remainder. Probably worthy of asking as another question.
Ruben Bartelink
A: 

I am sure there is an easier way but this might be a good excuse to take MGrammer for a spin.

zac
[Or not](http://www.douglaspurdy.com/2009/11/10/from-oslo-to-sql-server-modeling/) :P
Ruben Bartelink
Wow; hadn't seen that, thanks.
zac
Also look at the most recent post for balance (and the twitterverse for the opposing view :P)
Ruben Bartelink