1

I want to check if the dependencies of a project (right click on project and click "project dependencies") have been correctly added, programatically. How can I do this?

Precisely,

Qn- part 1) How can I access the "Dependencies" (not the references) of of a project in a solution?

Qn- part 2) How (rather where, as in csproj/sln...) should I check if all the dependencies for a project have been added as desired?

I tried rummaging through the csproj and sln files. I could not see a property/item group that, on parsing would give me a list of added dependencies.

Shoud I be using EnvDTE for this purpose. I am already using Microsoft.Build.Evaluation Class for thel bulk of other build operations I am doing. I dont know if that would complicate the process all the more.

Please advise. Thanks!!

aromore
  • 977
  • 2
  • 10
  • 25

2 Answers2

1

Visual Studio project files are really just XML, so you could theoretically load the file as an XmlDocument and select out all of the Reference nodes that you find. This is an example of how my references are stored in a sample VB.net project file. I imagine it's not too different in a .csproj file:

  <ItemGroup>
    <Reference Include="System.Data.Linq" />
    <Reference Include="System.Deployment" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Printing">
  </ItemGroup>

Once you are able to do that, it would be pretty trivial to build out a collection of reference names from the values in the Include attribute and then verify that it contains the references you are expecting.

Any explicit references that a project has are also implicit project depedencies. They are displayed, but not editable, in the Solution Properties window.

Edit

If you're looking for project dependencies for your solution, which were manually selected in addition to any explicit references you have, you can find those in the .sln file.

When you manually select a project dependency in the Solution Properties window, a ProjectSection(ProjectDependencies) section gets created inside of the Project's section.

For example, the following is a normal project entry in the .sln file:

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "MyProject",
"Source\MyProject\MyProject.vbproj",
"{AB5BA87B-77D5-4812-B02C-9B4FB87F4EF6}" EndProject

Here is what that project would look like if you were to manually add a project dependency later:

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "MyProject", "MyProject\MyProject.vbproj", "{AC1BE764-AC53-47B0-9608-648C28D3BB79}"
    ProjectSection(ProjectDependencies) = postProject
        {10F4313B-A586-4CC6-BAFE-51930EC8C68F} = {10F4313B-A586-4CC6-BAFE-51930EC8C68F}
    EndProjectSection
EndProject

Notice the addition of the ProjectSection(ProjectDependencies)

Unfortunately, unlike the project file, the solution file is not XML. This would make parsing the file directly as text more difficult. Also, the projects inside the solution file are associated to GUIDs and any sections that reference them later use that GUID, and not the project name. This also makes things a bit more complicated as you must do a little preprocessing on the solution file to create a map of GUID to Project name. At that point you would be able to list out any explicit project dependencies from the solution file and compare if the ones that were added were the ones you expected to be added.

mclark1129
  • 7,532
  • 5
  • 48
  • 84
  • yes. i want to know HOW to get the list of references (dependencies) i "expect" to be in that *evaluated include* list of references. – aromore Sep 05 '12 at 17:00
  • I'm not quite sure what you mean. You want to get the list of references that you'd expect to be in there? That's up to you, and based on how you determine the "expected" values. I've outlined how you can retrieve the entire list of references from the project file. Is your problem that you don't know how to work with XML in .NET? – mclark1129 Sep 05 '12 at 17:05
  • i am working on an application with 10 solutions and more than a 100 project files in them. i have retrieved the list of references u mentioned above using var listOfRefs = project.Items.Where(x=>x.ItemType=="Reference").ToList(); I want to access the project **DEPENDENCIES** (not necessarily the same as **references**) so that I could check if they exist in the listOfRefs. Get it?? My question is WHERE can i find **project dependencies** ?? – aromore Sep 05 '12 at 17:13
  • You should include the code that you are already using to parse the XML in your question. Also, please update your question to explain the difference in your mind between a "reference" or a "dependency" (an example of one vs the other would be great). I look at those two things as essentially the same thing, and do not understand how to differentiate the two. – mclark1129 Sep 05 '12 at 17:17
  • @aromore I have updated my question with an explanation of how project dependencies are stored inside of a solution file. – mclark1129 Sep 05 '12 at 17:47
1

This does what i want. I have parsed the sln file and extracted GUIDs of projects.

Community
  • 1
  • 1
aromore
  • 977
  • 2
  • 10
  • 25