Just-in-time updating of source files in an existing solution

Today a developer on my team tried to create an MsBuild task that would re-generate a few generated files during a Rebuild or as part of a Clean. Turned out to be a lot harder than we expected and it was mostly caused by an unknown feature that speeds up the build process inside Visual Studio. We actually want to regenerate the WCF Client proxies and the SPMetal generated code during build.

Visual Studio actually keeps the files that make up your solution in memory to improve its performance. MsBuild will actually detect that it's running inside Visual Studio and will grab the files from the in-memory cache instead of grabbing them from disk.

The problem is that any changes made to files in the solution during the build aren't picked up by MsBuild until the next time you build your project. The inner workings are explained in this post in the MSDN forums.

In our case we decided to make MsBuild use the on-disk files by setting the "UseHostCompilerIfAvailable" property to 'false' in the first propertygroup of the project file we were trying to patch on the fly.