MSBuild.GlobalEngine vs "new Engine()": What's the Difference?

Earlier this week we got the following question from one of our VSIP partners:

What is the difference between using MSBuild.GlobalEngine or “new Engine()” to build a project programmatically?

I actually never knew there were two ways to do this. Thankfully Rajeev not only knew there were, but knew what the difference was. His response also provides another glimpse into how Visual Studio and MSBuild interoperate:

Engine.GlobalEngine is just an instance of the Engine that was also created with "new Engine()", and the only point of it is so that multiple project systems in the same process (e.g., devenv.exe) can easily share the same engine object. The benefit in all the project systems sharing the same Engine object comes when you have project-to-project references between the various projects. If project A instantiated its own Engine object instead of using GlobalEngine, and it had a P2P reference to Project B, then Project A would end up loading a fresh copy of Project B from disk, instead of using the copy that was probably already loaded by the IDE as part of a different Engine object.

The main point is that projects in different Engine objects cannot talk to each other. So if you want projects to be able to talk to each other, it's best to have them part of the same Engine object, and the easiest way to do that for a VS package is to use the GlobalEngine.

[ Author: Neil Enns ]