Inject Code before Build
-
Wednesday, May 09, 2012 1:59 PM
Hi,
I am building a vspackage for Visual Studio 2010, and need to be able to inject code invisibly into every function in the solution during the build process.
Currently I can use this:
TextDocument doc = (elemInfo.ProjectItem.Document.Object() as TextDocument);
EditPoint ep = doc.CreateEditPoint(elemInfo.StartPoint); ep.Insert(sb.ToString());
which inserts the code directly into the code file for all to see. This works, but as the code only collects runtime data I never need to see it. Is there a way to inject the code during the build process without publically inserting the code like above?
I could create temporary files for all the code files in the solution, and inject the code into them, then dispose of them after the debug session ends. Would this be the best way?
When intellitrace collects data from the running executable, wouldn't it need to do something similiar to this? I assume it Injects code into each method to save variable states.
All Replies
-
Wednesday, May 09, 2012 4:56 PM
Hi Alex,
you should probably look at some AOP stuff (I have never had chance to test any of following personally)
- Some VS extension which brings some magic
http://visualstudiogallery.msdn.microsoft.com/074a2a26-d034-46f1-8fe1-0da97265eb7a
- PostSharp - will change code in some way during compilation or after it. I believe there are not changes required(in original code).
- Castle DynamicProxy - will use proxies/marshalling etc - has some impact on performance + requires code changes in original code
- Some advanced engines like custom language service + compiler
http://www.codeproject.com/Articles/12650/Using-AOP-in-C
+ many other framework listed here
http://csharp-source.net/open-source/aspect-oriented-frameworks
- Marked As Answer by lucy-liuModerator Thursday, May 17, 2012 6:13 AM

