In our last post we started to dig into how the Compile target is used by Visual Studio at design time, and the details around the in-process compiler. When Rajeev was talking about the in-process vs. out-of-process compiler during the talk I must admit I started to doze off a bit. I’m much more of a UI guy. That’s why I got so jazzed about how MSBuild plays a key role in Intellisense.

As mentioned in the previous post, when a project file is opened by Visual Studio, the Compile target is called to get a compile going and populate the Intellisense dropdown. The super cool part was Rajeev’s very visual demonstration of the relationship between MSBuild and Intellisense.

To see the demo for yourself, try the following:

  1. Create a new class library project
  2. Open the Class1.cs file and add the following code to the class:

    #if FOO_TXT_EXISTS
      bool myBoolean = false;
    #endif
  3. Edit the project file and add the following target after the <Imports> line:

    <Target Name="BeforeCompile">

      <CreateProperty Condition="Exists('c:\foo.txt')"
               
    Value="$(DefineConstants);FOO_TXT_EXISTS">

        <Output TaskParameter="Value" PropertyName="DefineConstants"/>

      </CreateProperty>
    </
    Target>
  4. Re-load the project and select the second option in the security dialog
  5. In the constructor for Class1 try typing “this.” and look for myBoolean in the Intellisense dropdown.

MyBoolean doesn’t show up! Why on earth not? Well, because the FOO_TXT_EXISTS define is only added to the DefineConstants property if the foo.txt file exists in the root of the c: drive. Since Visual Studio calls the MSBuild Compile task on project load to initialize Intellisense, and myBoolean is wrapped in a #if that isn’t set by the project file, myBoolean doesn’t exist after compilation.

Now let’s see what happens when the file exists. Create an empty file called “foo.txt” in the root of your c: drive, close your project, then open it again. Try typing “this.myBoolean” again in the class. Voila! Now it shows up in intellisense.

Very, very cool. Nice demo, Rajeev!

[ Author: Neil Enns ]