MSBuildWorkspace 获取嵌入式资源文件

MSBuildWorkspace Get Embedded Resource Files

我正在尝试使用 Roslyn 和 MSBuild Api.

从解决方案中取出所有嵌入的资源文件
private async Task<Document> CheckConstForLocalization(Document document, LocalDeclarationStatementSyntax localDeclaration,
    CancellationToken cancellationToken)
{
    foreach (var project in document.Project.Solution.Projects)
    {
        foreach (var sourceDoc in project.AdditionalDocuments)
        {
            if (false == sourceDoc.Name.EndsWith(".cs"))
            {
                Debug.WriteLine(sourceDoc.Name);
            }
        }

        foreach (var sourceDoc in project.Documents)
        {
            if (false == sourceDoc.Name.EndsWith(".cs"))
            {
                Debug.WriteLine(sourceDoc.Name);
            }
        }
    }

    var newRoot = await document.GetSyntaxRootAsync(cancellationToken);
    // Return document with transformed tree.
    return document.WithSyntaxRoot(newRoot);
}

当我将我的资源文件修改为AdditionFiles 时,我可以通过项目AdditionalDocuments 获取它们。但是我希望能够在不这样做的情况下抓住这些。该文件未出现在文档或其他文档中

如何在不修改属性的情况下找到 Resx 文件?

你现在不能。 API 不支持它。 (我昨天才研究这个。)

有一个 feature request 可以支持它,您可能想支持和订阅它,但我认为目前没有任何方法可以做到这一点。

我的理解是 Visual Studio 与 MSBuild 的连接比目前 Roslyn 的支持更紧密。 (另一个例子见 issue I raised about <Deterministic>。)

我找到了一种查找设计器文件的方法,我通过遍历 csproj 文件并获取嵌入式资源来获取关联的 C# 文档名称。

public const string LAST_GENERATED_TAG = "LastGenOutput";
public const string RESX_FILE_EXTENSION = ".resx";
public List<string> GetResourceDesignerInfo(Project project)
{
    XDocument xmldoc = XDocument.Load(project.FilePath);
    XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";

    var resxFiles = new List<string>();
    foreach (var resource in xmldoc.Descendants(msbuild + "EmbeddedResource"))
    {
        string includePath = resource.Attribute("Include").Value;

        var includeExtension = Path.GetExtension(includePath);
        if (0 == string.Compare(includeExtension, RESX_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase))
        {
            var outputTag = resource.Elements(msbuild +  LAST_GENERATED_TAG).FirstOrDefault();

            if (null != outputTag)
            {
                resxFiles.Add(outputTag.Value);
            }
        }
    }

    return resxFiles;
}