Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 296
AngryHacker Avatar asked Dec 05 '25 10:12

AngryHacker


2 Answers

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.

like image 73
Jon Skeet Avatar answered Dec 06 '25 22:12

Jon Skeet


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.

like image 25
Francisco Noriega Avatar answered Dec 07 '25 00:12

Francisco Noriega