BACKGROUND
我正在寻求创建一个Roslyn CodeFix,它将响应来自Visual Studio附带的内置分析器的诊断警告,该警告将识别未 - 或部分 - 实现的接口,允许我遍历缺少的成员,以及生成自定义代码,将代理调用委托给实现该接口的类型的Member字段 .
(随Visual Studio提供的Roslyn Analyzers和CodeFixs确实提供了这项功能,但我需要自定义和扩展代码的生成,这是不可能的,因为Microsoft实现都标记为 internal
. )
Please note: 该接口几乎总是位于外部第三方程序集中,我无权访问该源程序 .
例如从...开始:
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
...
}
WHAT I HAVE TRIED
我花了一些时间阅读关于Roslyn的语法树和语义模型功能的教程 .
我还检查了GitHub的Roslyn源代码 - 其中包括我希望实现的确切功能;但是,代码在各种复杂的类中交织在一起,并且实现为 internal
方法,这些方法不能被覆盖或扩展,或者实际上被提取到独立项目中 .
从调查多个样本,以及相关的SO问题How to see if a class has implemented the interface with Roslyn我得出结论,我必须使用Roslyn语义模型来获取有关接口的信息,并且它是声明的成员 .
一旦我可以获得接口成员,我就可以使用 SyntaxFactory
构建所需的输出代码,并且我已经使用'Roslyn Quoter'作为指导 .
从默认模板创建CodeFix,响应正确的诊断代码很简单,这是正常的 .
ISSUES
我遇到的问题是,使用诊断位置识别的令牌,该令牌似乎是 SimpleBaseTypeSyntax
令牌,并且
-
验证它实际上是代表一个接口,
-
获取符号定义,允许我枚举第三方接口的成员 .
语法Visualizer指示接口声明节点的类型为 SimpleBaseType
.
因此,我使用以下代码从语法树中获取令牌 SimpleBaseTypeSyntax
-
// Find the interface Syntax Token detected by the diagnostic.
var interfaceBaseTypeSyntax =
root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf()
.OfType<SimpleBaseTypeSyntax>().First();
a)这确实返回一个包含语法树中相关节点信息的标记 - 但是,我看不到任何'InterfaceTypeSyntax'类型或IsInterface方法来验证它实际上是一个接口 .
b)我相信我应该能够使用 semanticModel.GetSymbolInfo(interfaceBaseTypeSyntax)
,但是这总是返回null - 请记住接口是在外部程序集中声明的 .
我需要做些什么来通过GetSymbolInfo提供这些信息,还是我应该采取另一种方法......?
非常感谢你的建议......
2 回答
在这种情况下,您正在查看的语法节点是指类型而不是符号 . 如果传入的节点不是符号,则GetSymbolInfo将返回null . 你想使用
semanticModel.GetTypeInfo(interfaceBaseTypeSyntax)
.在发布后如此迅速地发现这一点相当尴尬,但解决方案似乎是指
Identifier
,它是SimpleBaseTypeSyntax
的后代 .并致电:
然后我可以使用
interfaceTypeInfo.Type.IsInterface
来验证我确实找到了一个接口类型,并且还可以访问interfaceTypeInfo.Type.GetMembers()
.答案是通过Syntax Visualizer盯着我看 .
I'm leaving this open for now, in case others have a better solution... thanks!