views:

103

answers:

2

I want to inject the following line into the top of every method of my application

Trace.WriteLine(this.GetType().Name + "." + "Name of Method");

I'd like to do it at compile time or build time or post-build - basically before it gets into customer's hands.

Is this possible?

+4  A: 

You should look into PostSharp which is designed for this sort of thing. I don't know whether it's got an attribute for exactly that use case already, but I would guess it wouldn't be hard to write one.

EDIT: Another thought is to try using Mono.Cecil which is a binary rewriter. I haven't used it myself, but it's worth a try.

Jon Skeet
I've looked into it, but it brings up the same exact problem. I'd have to go and decorate like a thousand classes. Which is a lesser problem then adding Trace.WriteLine to every method, but it's still a lot of repetitive work, which I am hoping to automate.
AngryHacker
+1  A: 

Yeah, you would use attributes for that, and as the Jon said, you could use PostSharp which is a great api for working easily with attributes.

Francisco Noriega