如何在 C# 代码生成器中阅读 XML 文档?

发布于 2025-01-14 01:25:51 字数 661 浏览 2 评论 0原文

我编写了一个简单的 C# 代码生成器,它实现了 Microsoft.CodeAnalysis.ISourceGenerator,它检查 enum 声明并根据用某些属性修饰的成员生成代码。它按预期工作,只是我无法在我注册的 ISyntaxContextReceiver 中检索任何 XML 文档注释。

我已经尝试过这篇文章中找到的解决方案,但是我是否检查枚举常量语法模型的主要琐事或在枚举常量的语义模型上调用 GetDocumentationCommentXml() 时,永远不会有任何 XML 文档。 (前一种技术总是生成一个带有单个 WhitespaceTrivia 项的琐事集合;后者总是返回一个空字符串。)

我怀疑这是因为 GeneratorExecutionContext 传递给生成器的Execute 方法将 ParseOptions.DocumentationMode 设置为 DocumentationMode.None,但我找不到任何方法来更改 ParseOptions。有谁知道如何做到这一点或者还有什么可以实现 XML 文档节点的检索?

I've written a simple C# code generator that implements Microsoft.CodeAnalysis.ISourceGenerator that inspects an enum declaration and generates code based on members that are decorated with certain attributes. It is working as expected except that I cannot retrieve—in the ISyntaxContextReceiver that I register—any XML documentation comments.

I've tried the solution found in this SO post, but whether I inspect the leading trivia on the enum constant's syntax model or call GetDocumentationCommentXml() on the enum constant's semantic model, there's never any XML documentation. (The former technique always yields a trivia collection with a single WhitespaceTrivia item; the latter always returns an empty string.)

I suspect that this is because the GeneratorExecutionContext passed to the generator's Execute method has ParseOptions.DocumentationMode set to DocumentationMode.None, but I can't find any way to change ParseOptions. Does anyone know how this can be done or what else might enable retrieval of XML documentation nodes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文