MSBuild in Visual Studio Part 10: What Does MSBuild Have To Do With Intellisense?
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:
Create a new class library project
Open the Class1.cs file and add the following code to the class:
#if FOO_TXT_EXISTS
bool myBoolean = false;
#endifEdit 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>Re-load the project and select the second option in the security dialog
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 ]
Comments
- Anonymous
May 21, 2007
Hi, I'm trying to override the intellisense comments (xml documetation file ex: System.Data.xml) to extend them. I already overridden ResolveAssemblyReferences Target (and Task) and tried the following code in RelatedFiles property. Thanks. Code to try to override System.Data.xml (failed)
public class ResolveAssemblyReference:Microsoft.Build.Tasks.ResolveAssemblyReference { ... ... [Output] public new ITaskItem[] RelatedFiles { get { List<ITaskItem> l = new List<ITaskItem>(base.RelatedFiles); foreach(ITaskItem it in ResolvedFiles) { if(it.ItemSpec.Contains("System.Data")) { ITaskItem iti = new TaskItem(@"C:tempSystem.Data.xml") it.CopyMetadataTo(iti); iti.SetMetadata("FusionName", ""); l.Add(iti); }
- Anonymous
August 17, 2008
PingBack from http://www.developerzen.com/2006/01/07/msbuild-in-visual-studio/