无法从 Microsoft.CodeAnalysis.Project

问题描述

我正在编写一些在 Visual Studio 之外运行的代码检查工具,并且我已经成功地使用 Roslyn 工具链打开解决方案、生成语义模型和语法树,并从大型代码生成各种输出

我希望将此工具链扩展到对 Xaml 文件的一些简单分析,但我一直在纠结如何访问 Xaml 文件

我有一个解决方案,其中包含一个项目文件,大致如下所示:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net452</TargetFramework>
    <UseWPF>true</UseWPF>
    <RootNamespace>XXX.XXX.Client</RootNamespace>
    <AssemblyName>XXX.XXX.Internal.UI.WPF</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
    <packagereference Include="markdig.Wpf" Version="0.4.0" />   
  </ItemGroup>
  <ItemGroup>
    <Reference Include="System.Speech" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="WindowsFormsIntegration" />
  </ItemGroup> 
</Project>

此项目文件包含代码和 xaml 文件的混合。我试图用大约这样的代码来阅读它:

using (var workspace = MSBuildWorkspace.Create(new Dictionary<string,string>
          {
                ["AlwaysCompileMarkupFilesInSeparateDomain"] = "false"
           }))
{
  workspace.SkipUnrecognizedProjects = true;
  workspace.LoadMetadataForReferencedProjects = true;
  Solution solution = await workspace.OpenSolutionAsync(opts.Project,new ConsoleProgressReporter());
  foreach (var projItem in solution.Projects)
  {
     if (projItem.AssemblyName.Contains(".UI.") || projItem.AssemblyName.Contains("Theme"))
     {
         // Get the Xaml files in the project and inspect them
     }
  }
}

我不知道如何获取实际的 Xaml 文件。我在 projItem.Documents 集合中看到 xaml 元素的 xxx.xaml.cs 文件和 xxx.g.cs 文件,但我无法弄清楚 xaml 文件在哪里。

AdditionalDocuments 枚举为空。我怀疑有一种方法可以让 Xaml 项目出现在 AdditionalDocuments 集合中,但我一直无法弄清楚它是什么。

目前,我正在使用一个 hack,我假设每个 xxx.xaml.cs 文件都有一个相应的 xxx.xaml 文件,但这不适用于没有代码的 xaml 元素(例如,一些资源词典)。

有没有更好的方法来做到这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)