获取.net中项目的项目依赖项

时间:2023-01-14 18:57:23

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-第1部分如何在解决方案中访问项目的“依赖关系”(而不是参考)?

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?

Qn-第2部分:我应该如何(而不是在csproj / sln ......中)检查是否已根据需要添加了项目的所有依赖项?

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.

我尝试翻阅csproj和sln文件。我看不到一个属性/项目组,在解析时会给我一个添加的依赖项列表。

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.

为了这个目的,我正在使用EnvDTE。我已经在使用Microsoft.Build.Evaluation类进行我正在进行的大量其他构建操作。我不知道这是否会使这个过程复杂化。

Please advise. Thanks!!

请指教。谢谢!!

2 个解决方案

#1


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:

Visual Studio项目文件实际上只是XML,因此理论上您可以将文件加载为XmlDocument并选择您找到的所有Reference节点。这是我的引用如何存储在示例VB.net项目文件中的示例。我想在.csproj文件中并没有太大的不同:

  <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.

一旦你能够做到这一点,从Include属性中的值构建一个引用名称集合然后验证它包含你期望的引用将是非常简单的。

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.

如果您正在寻找解决方案的项目依赖项,除了您拥有的任何显式引用外,还可以手动选择它们,您可以在.sln文件中找到它们。

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

在“解决方案属性”窗口中手动选择项目依赖项时,将在项目的部分内创建ProjectSection(ProjectDependencies)部分。

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

例如,以下是.sln文件中的正常项目条目:

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)

注意添加了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.

不幸的是,与项目文件不同,解决方案文件不是XML。这将使文件直接解析为文本更加困难。此外,解决方案文件中的项目与GUID相关联,以后引用它们的任何部分都使用该GUID,而不是项目名称。这也使事情变得更复杂,因为您必须对解决方案文件进行一些预处理,以创建GUID到Project名称的映射。此时,您将能够从解决方案文件中列出任何显式项目依赖项,并比较添加的项是否是您希望添加的项。

#2


0  

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

这就是我想要的。我解析了sln文件并提取了项目的GUID。

#1


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:

Visual Studio项目文件实际上只是XML,因此理论上您可以将文件加载为XmlDocument并选择您找到的所有Reference节点。这是我的引用如何存储在示例VB.net项目文件中的示例。我想在.csproj文件中并没有太大的不同:

  <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.

一旦你能够做到这一点,从Include属性中的值构建一个引用名称集合然后验证它包含你期望的引用将是非常简单的。

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.

如果您正在寻找解决方案的项目依赖项,除了您拥有的任何显式引用外,还可以手动选择它们,您可以在.sln文件中找到它们。

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

在“解决方案属性”窗口中手动选择项目依赖项时,将在项目的部分内创建ProjectSection(ProjectDependencies)部分。

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

例如,以下是.sln文件中的正常项目条目:

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)

注意添加了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.

不幸的是,与项目文件不同,解决方案文件不是XML。这将使文件直接解析为文本更加困难。此外,解决方案文件中的项目与GUID相关联,以后引用它们的任何部分都使用该GUID,而不是项目名称。这也使事情变得更复杂,因为您必须对解决方案文件进行一些预处理,以创建GUID到Project名称的映射。此时,您将能够从解决方案文件中列出任何显式项目依赖项,并比较添加的项是否是您希望添加的项。

#2


0  

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

这就是我想要的。我解析了sln文件并提取了项目的GUID。