C# 你如何找到一个接口的所有实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/620376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you find all implementations of an interface?
提问by iammichael
Suppose you have an interface defined in C#. What is the easiest method to find all classes that provide an implementation of the interface?
假设您有一个用 C# 定义的接口。查找提供接口实现的所有类的最简单方法是什么?
The brute force method would be to use "Find References" in Visual Studio and manually look through the results to separate out the usages from the implementations, but for an interface in a large codebase that is heavily referencedwith relatively few implementations, this can be time consuming and error prone.
蛮力方法是在 Visual Studio 中使用“查找引用”并手动查看结果以将用法与实现分开,但对于大型代码库中的接口,该接口被大量引用而实现相对较少,这可以是耗时且容易出错。
In Java, running javadoc on the codebase (using the -private option to include private classes) would generate a documentation page for the interface (e.g. Comparable) that includes all implementing classes for the interface as well as any subinterfaces (though it doesn't include implementing classes of the subinterfaces, these are relatively easy to determine by drilling down into the listed subinterfaces). It's this functionality that I'm looking for but with C# and Visual Studio.
在 Java 中,在代码库上运行 javadoc(使用 -private 选项包含私有类)将为接口(例如Comparable)生成一个文档页面,其中包括接口的所有实现类以及任何子接口(尽管它没有包括实现子接口的类,通过深入到列出的子接口,这些相对容易确定)。我正在寻找这个功能,但使用 C# 和 Visual Studio。
采纳答案by Jon Skeet
(Edit based on comment...)
(根据评论编辑...)
If you have ReSharper installed:
如果您安装了 ReSharper:
In Visual Studio, right click on the type name and choose "Go to Inheritor". Alternatively, select the type name, then go to ReSharper/View/Type Hierarchy to open up a new tab. (The menu will show you the keyboard shortcut - this can vary, which is why I explained how to find it :)
在 Visual Studio 中,右键单击类型名称并选择“转到继承者”。或者,选择类型名称,然后转到 ReSharper/View/Type Hierarchy 以打开一个新选项卡。(菜单将显示键盘快捷键 - 这可能会有所不同,这就是我解释如何找到它的原因:)
If you don't have ReSharper:
如果您没有 ReSharper:
回答by Mohit Chakraborty
回答by Canavar
If you use resharper ALT + ENDshortcut might help to find all Inheritors.
如果您使用 resharper ALT + END快捷方式可能有助于找到所有继承者。
回答by overslacked
回答by phoose
You can right click a method name (definition in interface or implementation in other class) and choose View Call Hierarchy. In Call Hierarchy window there is "Implements" folder where you can find all locations of the interface method implementation.
您可以右键单击方法名称(在接口中定义或在其他类中的实现)并选择查看调用层次结构。在 Call Hierarchy 窗口中有“Implements”文件夹,您可以在其中找到接口方法实现的所有位置。
回答by P.Brian.Mackey
With Visual Studio 2010+
使用 Visual Studio 2010+
Right click a member method and choose view call hierarchy. Expand the Implements folder. This lists all the types that implement the interface the method belongs to.
右键单击成员方法并选择查看调用层次结构。展开“实现”文件夹。这列出了实现该方法所属接口的所有类型。
With Resharper 7
使用 Resharper 7
Right Click the interface > Navigate To > Derived Symbols. The symbols listed in boldderive directly from the interface. Non-bold symbols derive from a superclass.
右键单击界面 > 导航至 > 派生符号。以粗体列出的符号直接来自界面。非粗体符号源自超类。
回答by Akira Yamamoto
Use Shift + F12 to show all references, including the definitions.
使用 Shift + F12 显示所有引用,包括定义。
回答by Kees C. Bakker
You could do a regular expression search for the interface.
您可以对接口进行正则表达式搜索。
:(\ *[^},]+,)*\ *IMyInterfaceName
CTRL+SHIFT+F launches the following window:
CTRL+SHIFT+F 启动以下窗口:
回答by Tony L.
I prefer the "Navigate To..." option. With your cursor on the function call, try the following:
我更喜欢“导航到...”选项。将光标放在函数调用上,尝试以下操作:
Shortcut Key:
快捷键:
- Ctrl+, (Ctrl+comma)
- Ctrl+, (Ctrl+逗号)
Menu:
菜单:
- Edit Menu
- Click "Navigate To..."
- 编辑菜单
- 单击“导航到...”
Benefits:
好处:
- Doesn't show all references like "Find All References"
- Shows the "type" of the implementation so it will note which is your interface
- 不显示所有引用,如“查找所有引用”
- 显示实现的“类型”,以便它会注意到哪个是您的接口
回答by Nik A.
For those using Visual Studio 2015, there is this awesome extension called Go To Implementation. Give it a try.
对于那些使用 Visual Studio 2015 的人,有一个很棒的扩展名为Go To Implementation。试一试。
Once you've installed the extension, you can just right click at any occurrences of the interface (e.g. IUrlShortener) and click on Go To Implementation
menu. If you only have one class that implements the interface, clicking the menu will bring you directly to the class. If you have more than one class that implements the interface, it will list all the classes.
安装扩展程序后,您只需右键单击界面的任何位置(例如 IUrlShortener),然后单击Go To Implementation
菜单。如果您只有一个实现该接口的类,单击菜单将直接带您进入该类。如果您有多个实现该接口的类,它将列出所有类。