使用Roslyn打开Visual Studio解决方案

时间:2017-11-21 04:44:27

标签: visual-studio roslyn

我正在尝试使用Roslyn打开Visual Studio解决方案来迭代项目和文档。我不想写任何东西。使用下面的代码NewSolution.Projects.count始终为0.最后,如果重要的话,我会想要为每个文档获取SemanticModel。

    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

1 个答案:

答案 0 :(得分:2)

您实际上从未打开过现有的解决方案文件。 SolutionInfo.FilePath用于标识解决方案并解析相对路径,但从未实际从磁盘读取任何内容(特别是,核心Workspaces API对任何特定文件格式都不了解)。

要阅读.sln个文件和基于MSBuild的.cs-proj文件,您需要MSBuildWorkspace

相关问题