AngryHacker
AngryHacker

Reputation: 61636

Is there a way to inject code at build time?

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?

Upvotes: 3

Views: 791

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1502386

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.

Upvotes: 5

Francisco Noriega
Francisco Noriega

Reputation: 14604

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.

Upvotes: 1

Related Questions