Wednesday, November 01, 2006

Nested files with 'DependentUpon' in Visual Studio

Here's a really neat trick discovered by my colleague Preet that you can use to show a relationship between files in Visual Studio, just like Microsoft does with partial 'designer' classes. You've probably noticed when you add a new windows form in VS 2005 that two files are created: MyForm.cs and MyForm.designer.cs. The designer file contains all the code that's generated by the form designer and the other file is where you write your user code. Preet showed me that you can do the same thing by modifying the .csproj file (aka the MSBuild file) and add a DependentUpon child element to any item that you want to appear below another. Here's a snippet of a .csproj that has three files, all partial classes of 'Foo'. In the solution explorer Foo.1.cs appears nested below Foo.cs and Foo.1.1.cs appears below Foo.1.cs.

  
  <ItemGroup>
    <Compile Include="Foo.1.1.cs">
      <DependentUpon>Foo.1.cs</DependentUpon>
    </Compile>
    <Compile Include="Foo.1.cs">
      <DependentUpon>Foo.cs</DependentUpon>
    </Compile>
    <Compile Include="Foo.cs" />
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>

The only way of doing this at the moment is by editing the .csproj file, but it's pretty cool, especially if you're writing code generators and GAT tools like I have been recently where you might want generated content to be visually related to user written code.

7 comments:

Joannes Vermorel said...

Do you know if the DependentUpon tag has any semantic impact on the build process? Does it only impact the VS display?

Best regards,
Joannès
blog.lokad.com

Anonymous said...

When I make a new < SubType >Code< /SubType > file DependentUpon a form type file, studio insists on rewriting my project file to change the type of the dependency into form as well.

Do you by any chance know if this is a bug or if there is some trick to preserving the dependent file type?

Anonymous said...

you can download free addin to visual studio from here: http://www.kooriyoo.com/VSCommands/Default.aspx
and group items from VS IDE without need to add DependentUpon manually to project file.

Anonymous said...

This should also work with SubTypes, but I placed the new Item behind other dependent files, like the C#-Files.

For Example:
<Page Include="HomePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>

<Compile Include="HomePage.xaml.cs">
<DependentUpon>HomePage.xaml</DependentUpon>
</Compile>

<Page Include="HomePage.MyOwnResources.xaml">
<DependentUpon>HomePage.xaml</DependentUpon>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>

Anonymous said...

someone posted the link to visual studio addin. the link appears to be broken. i don't know if it's same addin, but I found one here http://www.mokosh.co.uk/post/VsCommands-12-Released.aspx

Anonymous said...

Updated link to the tool:
http://mokosh.co.uk/vscommands/

Unknown said...

One thing that the original article doesn't point out is that you shouldn't include the path of the "parent" item on the child item. For example, if my object BaseEntity was in Entities\BaseEntity.cs, you'd do:

DependentUpon BaseEntity.cs /DependentUpon

Notice that I don't put Entities\BaseEntity.cs in my DependentUpon.

Sorry the brackets aren't coming through because of HTML formatting.