简体   繁体   English

使用 Roslyn 获取方法的实现

[英]Get method's implementation with Roslyn

If for example, I declare an interface class whose actual class type will only be determined on runtime, and by using VisualStudio when I right click the method call and click "Go To Implementation" I can find 3 implementations from classes that inherits the interface.例如,如果我声明了一个接口类,其实际类类型仅在运行时确定,当我右键单击方法调用并单击“转到实现”时,通过使用 VisualStudio,我可以从继承接口的类中找到 3 个实现。

How can I, using Roslyn, get the method's implementation syntax nodes/symbols?我如何使用 Roslyn 获取方法的实现语法节点/符号?

Just look at the code that implements Go To Implementation .只需查看实现 Go To Implementation代码即可 There's several public APIs on the SymbolFinder type that give you overrides/implementations/derived types, and each give you symbols back. SymbolFinder类型上有几个公共 API,可以为您提供覆盖/实现/派生类型,并且每个 API 都会返回符号。 Most of the code in the feature is just figuring out which is the right method to call, with some extra filtering that's specific for the feature.该功能中的大部分代码只是确定哪个是正确的调用方法,并带有一些特定于该功能的额外过滤。 For example, if you have a derived type that's got an abstract member, the core functions will return that method, but it's not an implementation in a meaningful way for the sake of the feature.例如,如果您有一个具有抽象成员的派生类型,核心函数将返回该方法,但就该功能而言,它不是一种有意义的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM