Visual studio 使用Roslyn打开Visual Studio解决方案
我正试图与Roslyn一起打开一个Visual Studio解决方案,以遍历项目和文档。我什么都不想写。下面的代码NewSolution.Projects.count始终为0。如果有必要的话,我最终会希望获得每个文档的语义模型Visual studio 使用Roslyn打开Visual Studio解决方案,visual-studio,roslyn,Visual Studio,Roslyn,我正试图与Roslyn一起打开一个Visual Studio解决方案,以遍历项目和文档。我什么都不想写。下面的代码NewSolution.Projects.count始终为0。如果有必要的话,我最终会希望获得每个文档的语义模型 Const SolutionPartialPath As String = "Visual Studio 2017\Projects\roslyn-master\src\Samples\Samples.sln" <TestMethod()> P
Const SolutionPartialPath As String = "Visual Studio 2017\Projects\roslyn-master\src\Samples\Samples.sln"
<TestMethod()> Public Sub UnitTest1()
Dim myDoc As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim SampleSolutionPath As String = Path.Combine(myDoc, SolutionPartialPath)
Dim WS As AdhocWorkspace = New AdhocWorkspace()
Dim SolInfo As SolutionInfo = SolutionInfo.Create(SolutionId.CreateNewId, VersionStamp.Create, SampleSolutionPath)
Dim NewSolution As Solution = WS.AddSolution(SolInfo)
For Each project As Project In NewSolution.Projects
Debug.WriteLine(project.Name)
If project.Name = "BasicAnalyzers" Then
For Each document In project.Documents
Debug.WriteLine(document.Name)
ProcessOneFile(document)
Next document
End If
Next project
End Sub
Const SolutionPartialPath As String=“Visual Studio 2017\Projects\roslyn master\src\Samples\Samples.sln”
公共子单元测试1()
Dim myDoc As String=My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim SampleSolutionPath为String=Path.Combine(myDoc,SolutionPartialPath)
Dim WS As AdhocWorkspace=新的AdhocWorkspace()
将SolInfo设置为SolutionInfo=SolutionInfo.Create(SolutionId.CreateNewId,VersionStamp.Create,SampleSolutionPath)
Dim NewSolution As Solution=WS.AddSolution(SolInfo)
作为NewSolution.Projects中的项目用于每个项目
Debug.WriteLine(project.Name)
如果project.Name=“BasicAnalyzers”,则
用于项目中的每个文档。文档
Debug.WriteLine(document.Name)
ProcessOneFile(文档)
下一份文件
如果结束
下一个项目
端接头
您从未实际打开过现有解决方案文件。
SolutionInfo.FilePath
用于识别解决方案和解析相对路径,但从未实际从磁盘读取任何内容(特别是,核心工作区API不知道任何特定文件格式)
要读取
.sln
文件和基于MSBuild的.cs proj
文件,您需要MSBuildWorkspace
您从未实际打开过现有的解决方案文件。
SolutionInfo.FilePath
用于识别解决方案和解析相对路径,但从未实际从磁盘读取任何内容(特别是,核心工作区API不知道任何特定文件格式)
要阅读
.sln
文件和基于MSBuild的.cs proj
文件,您需要MSBuildWorkspace
谢谢,我可以打开解决方案并遍历项目,但项目中没有文档。我查看了WS.OpenProjectAsync(project.FilePath).Result,但这只是得到一个关于工作区中已有文档的错误。程序似乎正确,问题是错误“找不到指定的SDK'Microsoft.NET.SDK'。除非添加AddHandler MS_Workspace.WorkspaceFailed,Sub(发件人作为对象,e作为WorkspaceDiagnosticEventArgs),否则不会出现此错误。现在我需要纠正这个问题。谢谢,我能够打开解决方案并遍历项目,但项目中没有文档。我查看了WS.OpenProjectAsync(project.FilePath)。结果是,工作区中已经存在一个关于文档的错误。程序似乎正确,问题在于错误“找不到指定的SDK‘Microsoft.NET.SDK’。”除非添加AddHandler MS_Workspace.WorkspaceFailed,Sub(发件人作为对象,e作为WorkspaceDiagnosticEventArgs),否则不会出现此问题。现在我需要更正此问题。