Roslyn 2.x 实现缺少接口的 CodeFix,委托给成员,VS 2017
Roslyn 2.x CodeFix that implements a missing interface, delegated to a member, VS 2017
背景
我正在寻求创建一个 Roslyn CodeFix,它将响应来自 Visual Studio 附带的内置分析器的诊断警告,它将识别未实现或部分实现的接口,允许我遍历缺少的成员,并生成自定义代码,将方法调用委托给实现接口的类型的成员字段。
(Visual Studio 附带的 Roslyn Analyzers 和 CodeFixes 确实提供了此功能,但我需要自定义和扩展代码的生成,这是不可能的,因为 Microsoft 实现都标记为 internal
.)
请注意:接口几乎总是位于外部的第三方程序集中,我无法访问它的源代码。
例如起始于:
public class CustomDocumentDBClient : IDocumentClient
{
}
期望的结果将类似于以下内容(在实践中,一旦基本原则起作用,我将创建多个版本来添加额外的代码来包装方法调用):
public class CustomDocumentDBClient : IDocumentClient
{
// Field to which calls are delegated, initialised by the constructor
private readonly IDocumentClient _documentClientImplementation;
// Constructor
public CustomDocumentDBClient (IDocumentClient documentClientImplementation)
{
_documentClientImplementation = documentClientImplementation;
}
// Interface method that delegates to private field for invocation
public Task<ResourceResponse<Attachment>> CreateAttachmentAsync(string documentLink, object attachment, RequestOptions options = null)
{
return _documentClientImplementation.CreateAttachmentAsync(documentLink, attachment, options);
}
// Interface method that delegates to private field for invocation
public Task<ResourceResponse<Attachment>> CreateAttachmentAsync(string documentLink, Stream mediaStream, MediaOptions options = null, RequestOptions requestOptions = null)
{
return _documentClientImplementation.CreateAttachmentAsync(documentLink, mediaStream, options, requestOptions);
}
...
other methods
...
}
我尝试了什么
我花了一些时间阅读有关 Roslyn 的语法树和语义模型功能的教程。
我还检查了来自 GitHub 的 Roslyn 源代码 - 确实 包含我希望实现的确切功能;然而,代码在各种复杂的 类 中交织在一起,并作为 internal
方法实现,无法覆盖或扩展,或者实际上无法提取到独立项目中。
通过调查多个样本以及一个相关的 SO 问题 我得出结论,我必须使用 Roslyn 语义模型来获取有关接口及其声明成员的信息。
一旦我可以获得接口成员,我就可以使用 SyntaxFactory
构建所需的输出代码,并且我已经使用 'Roslyn Quoter' 作为指导。
从默认模板创建 CodeFix,以响应正确的诊断代码非常简单,并且正在运行。
问题
我遇到的问题是使用诊断位置标识的令牌,它似乎是 SimpleBaseTypeSyntax
令牌,并且
- 验证它实际上代表一个接口,
- 获取允许我枚举第三方接口成员的符号定义。
语法可视化工具指示接口声明节点的类型为 SimpleBaseType
。
因此,我使用以下代码从语法树中获取令牌 SimpleBaseTypeSyntax
-
// Find the interface Syntax Token detected by the diagnostic.
var interfaceBaseTypeSyntax =
root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf()
.OfType<SimpleBaseTypeSyntax>().First();
a) 这会 return 包含语法树中相关节点信息的标记 - 但是,我看不到任何 'InterfaceTypeSyntax' 类型或 IsInterface 方法来验证它实际上是一个接口。
b) 我相信我应该能够使用 semanticModel.GetSymbolInfo(interfaceBaseTypeSyntax)
,但是这总是 returns null - 请记住接口是在外部程序集中声明的。
我需要做些什么才能通过 GetSymbolInfo 获取该信息,还是我应该采取另一种方法...?
非常感谢您的建议...
在这种情况下,您正在查看的语法节点指的是类型而不是符号。如果您传入的节点不是符号,GetSymbolInfo 将 return null。你想使用 semanticModel.GetTypeInfo(interfaceBaseTypeSyntax)
。
发帖后这么快就找到了,有点尴尬,不过解决办法好像是参考SimpleBaseTypeSyntax
的后代Identifier
。
var interfaceSymbolInfo =
semanticModel.GetSymbolInfo(interfaceBaseTypeSyntax.DescendantNodes().First());
并且,通过调用:
var interfaceTypeInfo =
semanticModel.GetTypeInfo(interfaceBaseTypeSyntax.DescendantNodes().First());
然后我可以使用 interfaceTypeInfo.Type.IsInterface
来验证我确实找到了接口类型,并且还可以访问 interfaceTypeInfo.Type.GetMembers()
.
通过语法可视化工具,答案就在眼前。
我暂时将此打开,以防其他人有更好的解决方案...谢谢!
背景
我正在寻求创建一个 Roslyn CodeFix,它将响应来自 Visual Studio 附带的内置分析器的诊断警告,它将识别未实现或部分实现的接口,允许我遍历缺少的成员,并生成自定义代码,将方法调用委托给实现接口的类型的成员字段。
(Visual Studio 附带的 Roslyn Analyzers 和 CodeFixes 确实提供了此功能,但我需要自定义和扩展代码的生成,这是不可能的,因为 Microsoft 实现都标记为 internal
.)
请注意:接口几乎总是位于外部的第三方程序集中,我无法访问它的源代码。
例如起始于:
public class CustomDocumentDBClient : IDocumentClient
{
}
期望的结果将类似于以下内容(在实践中,一旦基本原则起作用,我将创建多个版本来添加额外的代码来包装方法调用):
public class CustomDocumentDBClient : IDocumentClient
{
// Field to which calls are delegated, initialised by the constructor
private readonly IDocumentClient _documentClientImplementation;
// Constructor
public CustomDocumentDBClient (IDocumentClient documentClientImplementation)
{
_documentClientImplementation = documentClientImplementation;
}
// Interface method that delegates to private field for invocation
public Task<ResourceResponse<Attachment>> CreateAttachmentAsync(string documentLink, object attachment, RequestOptions options = null)
{
return _documentClientImplementation.CreateAttachmentAsync(documentLink, attachment, options);
}
// Interface method that delegates to private field for invocation
public Task<ResourceResponse<Attachment>> CreateAttachmentAsync(string documentLink, Stream mediaStream, MediaOptions options = null, RequestOptions requestOptions = null)
{
return _documentClientImplementation.CreateAttachmentAsync(documentLink, mediaStream, options, requestOptions);
}
...
other methods
...
}
我尝试了什么
我花了一些时间阅读有关 Roslyn 的语法树和语义模型功能的教程。
我还检查了来自 GitHub 的 Roslyn 源代码 - 确实 包含我希望实现的确切功能;然而,代码在各种复杂的 类 中交织在一起,并作为 internal
方法实现,无法覆盖或扩展,或者实际上无法提取到独立项目中。
通过调查多个样本以及一个相关的 SO 问题
一旦我可以获得接口成员,我就可以使用 SyntaxFactory
构建所需的输出代码,并且我已经使用 'Roslyn Quoter' 作为指导。
从默认模板创建 CodeFix,以响应正确的诊断代码非常简单,并且正在运行。
问题
我遇到的问题是使用诊断位置标识的令牌,它似乎是 SimpleBaseTypeSyntax
令牌,并且
- 验证它实际上代表一个接口,
- 获取允许我枚举第三方接口成员的符号定义。
语法可视化工具指示接口声明节点的类型为 SimpleBaseType
。
因此,我使用以下代码从语法树中获取令牌 SimpleBaseTypeSyntax
-
// Find the interface Syntax Token detected by the diagnostic.
var interfaceBaseTypeSyntax =
root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf()
.OfType<SimpleBaseTypeSyntax>().First();
a) 这会 return 包含语法树中相关节点信息的标记 - 但是,我看不到任何 'InterfaceTypeSyntax' 类型或 IsInterface 方法来验证它实际上是一个接口。
b) 我相信我应该能够使用 semanticModel.GetSymbolInfo(interfaceBaseTypeSyntax)
,但是这总是 returns null - 请记住接口是在外部程序集中声明的。
我需要做些什么才能通过 GetSymbolInfo 获取该信息,还是我应该采取另一种方法...?
非常感谢您的建议...
在这种情况下,您正在查看的语法节点指的是类型而不是符号。如果您传入的节点不是符号,GetSymbolInfo 将 return null。你想使用 semanticModel.GetTypeInfo(interfaceBaseTypeSyntax)
。
发帖后这么快就找到了,有点尴尬,不过解决办法好像是参考SimpleBaseTypeSyntax
的后代Identifier
。
var interfaceSymbolInfo =
semanticModel.GetSymbolInfo(interfaceBaseTypeSyntax.DescendantNodes().First());
并且,通过调用:
var interfaceTypeInfo =
semanticModel.GetTypeInfo(interfaceBaseTypeSyntax.DescendantNodes().First());
然后我可以使用 interfaceTypeInfo.Type.IsInterface
来验证我确实找到了接口类型,并且还可以访问 interfaceTypeInfo.Type.GetMembers()
.
通过语法可视化工具,答案就在眼前。
我暂时将此打开,以防其他人有更好的解决方案...谢谢!