从.sln文件解析ProjectDependency [英] Parse ProjectDependency from .sln file

查看:163
本文介绍了从.sln文件解析ProjectDependency的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010

Project
("{00000000-0000-0000-0000-000000000000}") = "Test Projects", "TestProjects", "{5FAF487E-B913-4D46-983E-CB13FDF36FCB}" 
EndProject

Project
("{11111111-1111-1111-1111-111111111111}")= "ProjPropHelperAPI", "ProjPropHelperAPI\ProjPropHelperAPI.vcxproj", "{22222222-2222-2222-2222-222222222222}" 
EndProject 

Project
("{33333333-3333-3333-3333-333333333333}") = "ProjectPropertiesHelperAPI", "ProjectPropertiesHelperAPI\ProjectPropertiesHelperAPI.csproj", "{66666666-6666-6666-6666-666666666666}"     
    ProjectSection(ProjectDependencies) = postProject         
    {44444444-4444-4444-4444-444444444444} = {44444444-4444-4444-4444-444444444444}    
    {55555555-5555-5555-5555-555555555555} = {55555555-5555-5555-5555-555555555555}
    EndProjectSection 
EndProject 



我可以使用此答案中提到的解决方案来阅读项目名称,其GUID和路径.名称,GUID和路径使用
访问



I can read the Project Names, their GUIDs and Paths using the solution mentioned in this answer. The name, guid and path is accessed using

Type.GetType().GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance); 



我需要解析以上.sln摘录中显示的ProjectDependencies选项卡.我想知道是否可以获取类似于ProjectName,ProjectGuid和RelativePath属性的ProjectDependencies值?

P.S.好吧,只要通过任何方式获得项目依赖项,我都不会介意(不一定要通过上面的c#片段中所示的Reflection来实现).任何想法都欢迎.

在此先感谢!!



I need to parse the ProjectDependencies Tab shown in the above .sln extract. I want to know if the ProjectDependencies values can be obtained similar to ProjectName, ProjectGuid and RelativePath properties?

P.S. Well, I dont really mind as long as I get the project dependencies, by whatever means (not necessarily by Reflection as shown in the c# snippet above). Any ideas are welcome.

Thanks in advance!!

推荐答案

至少有两个完全不同的事物可能会产生不同的结果.这取决于您的实际需求. (从解释您的最终目的开始提出所有问题是一个好主意.)

第一个是:您可以按照解决方案文件中的说明提取依赖项信息.这并不反映实际的依赖关系,它仅显示解决方案的作者认为应该依赖于什么的内容(以及Visual Studio能够自动找出的依赖关系).这种方式不需要构建项目,也不需要将目标程序集放置到位.为此,您所需要的只是从文本(.SLN文件的文本)中解析简单数据并应用一些逻辑的能力.您可以为样本创建几种不同的解决方案,以查看文件的结构. 与基于XML的MSBuild项目文件格式不同,.SLN文件的格式并未真正记录. 即使.SLN文件格式可能未记录,您也可以不直接使用访问数据,但使用解决方案文档界面EnvDTE80.Solution2;请参阅解决方案2.[END EDIT]

第二件事是:您可以构建所有解决方案(因此您将需要所有项目都是可构建的),并使用反射功能基于已编译的二进制程序集执行依赖性分析.这是一件非常简单的事情.这是您需要的:
根据其主要可执行模块的文件名加载一些要分析的程序集:
http://msdn.microsoft.com/en-us/library/1009fa28.aspx [ ^ ].

完成后,获取所有引用的程序集:
http://msdn.microsoft.com/en-us/library/system. Reflection.assembly.getreferencedassemblies.aspx [ ^ ].

对于每个引用的程序集,请使用在上一步中为每个引用的程序集获得的程序集名称对象加载它:
http://msdn.microsoft.com/en-us/library/system.reflection. assemblyname.aspx [^ ].

通过它们各自的System.Reflection.AssemblyName:
实例加载每个引用的程序集 http://msdn.microsoft.com/en-us/library/x4cw969y.aspx [ ^ ].

递归执行所有操作以生成整个依赖关系图.很简单.

—SA
There are can be at least two completely different things which may give different results. It depends on what you really need. (It''s a good idea to start all questions from explanation of your ultimate purpose.)

First one is: you can extract dependency information as it is prescribed in the solution file. This does not reflect real dependency, it only shows what the author of the solution thinks what should depend on what (plus dependencies Visual Studio was able to figure out automatically). This way does not require the projects to be built, and you don''t need the target assemblies to be in place. To do it, all you need is just the ability to parse simple data from text (a text of a .SLN file) and apply some logic. You can create few different solutions for samples, to see how the file is structured. Unlike XML-based MSBuild project file format, the format of the .SLN files is not really documented. Even though the .SLN file format is probably not documented, you can use access data not directly, but using the solution document interface EnvDTE80.Solution2; please see Solution 2. [END EDIT]

The second thing is: you can build all the solution (so you will need all projects to be buildable) and perform the analysis of dependencies based on the compiled binary assemblies using Reflection. This is a pretty simple thing. Here is what you need:
Load some assembly to be analyzed based on the file name of its main executable module:
http://msdn.microsoft.com/en-us/library/1009fa28.aspx[^].

When this is done, obtain all referenced assemblies:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies.aspx[^].

For each referenced assembly, load it using the assembly name object obtained for each referenced assembly on the previous step:
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx[^].

Load each of referenced assemblies by their respective instanced of System.Reflection.AssemblyName:
http://msdn.microsoft.com/en-us/library/x4cw969y.aspx[^].

Do it all recursively to produce the whole dependency graph. Easy enough.

—SA


除了Sergey提到的内容,您还可以查看
Solution2界面 [项目界面 [ ^ ]

控制项目和解决方案 [
Apart from what Sergey mentions, you could look at the
Solution2 interface[^] and the Project interface[^]

Controlling Projects and Solutions[^]

It''s a lot of info to wade through, but what you''re looking for is there somewhere.

Best regards
Espen Harlinn


如果要解析文件,则可以执行以下操作:
If you go parsing the files, you may do the following:

    从这些条目
  1. 为所有Project...ProjectEnd条目
  2. 解析sln文件采用GUID,并且从这些条目的路径(如果它以\.\w+proj结尾)
  3. 也采用ProjectSection(ProjectDependencies) = postProject(如果存在并且将包含的GUID保存为依赖项)
  4. <ProjectReference>...</ProjectReference>解析每个proj文件,并将所有包含的<Project>...</Project>条目作为GUID到依赖项目
  5. 对依赖项进行拓扑排序

  1. parse the sln file for all Project...ProjectEnd entries
  2. from these entries take the GUID and the path (if it ends on \.\w+proj)
  3. from these entries also take the ProjectSection(ProjectDependencies) = postProject (if it exists and store the contained GUIDs as dependencies)
  4. parse each proj file for <ProjectReference>...</ProjectReference> and take all contained <Project>...</Project> entries as GUID to the dependent project
  5. do a topological sort on the dependencies



上面的步骤尊重Visual Studio可以在两个位置存储依赖项信息的事实:在解决方案中和在项目文件中.它还要注意sln文件中的某些项目条目是不是真实项目(它们可能是文件夹和解决方案项)的事实.

对于DAG/拓扑排序,请参见
有向无环图 [



The steps above respect the fact that Visual Studio may store dependency information at two locations: in the solution and in the project file. It also takes care of the fact that some project entries in the sln file are not real projects (they may be folders and solution items).

For the DAG/topological sort, see Directed Acyclic Graphs[^]

Cheers
Andi


这篇关于从.sln文件解析ProjectDependency的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆