C# 如何快速找到接口方法的实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261567/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:22:13  来源:igfitidea点击:

How do you quickly find the implementation(s) of an interface's method?

c#visual-studiointellisenseinterface-implementation

提问by Beep beep

Is there a quick way to find all of the implementations of, not references to, an interface's method/property/etc? Here's some sample code:

有没有一种快速的方法来查找接口的方法/属性/等的所有实现,而不是引用?这是一些示例代码:

public class SomeClass : IBaseClass
{
  public Int32 GetInt()
  {
     return 1;
  }
}

public interface IBaseClass
{
  public Int32 GetInt();
}

public class SomeOtherClass
{
  IBaseClass _someClass;
  private TestMethod()
  {
    _someClass = new SomeClass();
    _someClass.GetInt();
  }
}

I want to quickly get to SomeClass.GetInt() while reviewing SomeOtherClass.TestMethod(). If I right click on _someClass.GetInt() and click 'Go To Definition', it takes me to the interface. If I click 'Find All References', I could potentially see a list of all uses ... not just the classes that implement the GetInt() method.

我想在查看 SomeOtherClass.TestMethod() 的同时快速访问 SomeClass.GetInt()。如果我右键单击 _someClass.GetInt() 并单击“转到定义”,它会将我带到界面。如果我单击“查找所有引用”,我可能会看到所有用途的列表……而不仅仅是实现 GetInt() 方法的类。

Is there a faster way to find this? Any tips from other developers? We are using D.I. for most of our dependencies, which means that tracing through deeply nested code takes forever.

有没有更快的方法来找到这个?来自其他开发者的任何提示?我们对大多数依赖项使用 DI,这意味着跟踪深度嵌套的代码需要永远

回答by lance

Alt-End will do this in ReSharper, I believe.

我相信,Alt-End 会在 ReSharper 中做到这一点。

回答by Brian Rasmussen

R# has a Go to Implementation option on the pop-up menu, which is really handy for that.

R# 在弹出菜单上有一个 Go to Implementation 选项,这真的很方便。

回答by Zhenya

Without ReSharper the best way to do this is:

如果没有 ReSharper,最好的方法是:

Find in files (Ctrl+Shift+F) Find What: "class*ISomeClass" Find Options: "Use Wildcards"

在文件中查找 (Ctrl+Shift+F) 查找内容:“class*ISomeClass” 查找选项:“使用通配符”

This will find all implementation and than you can search for your function in a concrete implementation.

这将找到所有实现,然后您可以在具体实现中搜索您的功能。

回答by Arialdo Martini

Since I don't like to use the mouse while coding, I usually

由于我不喜欢在编码时使用鼠标,我通常

  • move the cursor over the method
  • type ctrl+k clrl+tto open the Call Hierarchy window
  • move down to Implements node.
  • type Return to go to the selected implementation
  • 将光标移到方法上
  • 输入ctrl+k clrl+t打开 Call Hierarchy 窗口
  • 向下移动到实现节点。
  • 键入 Return 以转到选定的实现

AFAIK this is the quickest way to find the implementation of a method, without ReSharper.

AFAIK 这是在没有 ReSharper 的情况下找到方法实现的最快方法。

(By the way: you can use the same system to move from a class method implementation to the corresponding interface declaration: just select the root)

(顺便说一句:你可以使用同一个系统从一个类方法实现移动到相应的接口声明:只需选择根)

回答by BlackHymanetMack

If your interface is in the same library as your concrete model that you'll typically want to navigate to, you could add a 'using alias' to the concrete object. 'Usings' are helpers anyhow, and this helps.

如果您的界面与您通常想要导航到的具体模型在同一个库中,您可以向具体对象添加一个“使用别名”。无论如何,“使用”都是帮手,这会有所帮助。

using System;
using PrimaryImplementation = YourNamespace.YourConcreteObject;


public interface IYourInterface{

}

When you're taken to the interface, you have a quick (and dirty) way of getting to the primary implementation of your interface. F12->F12!

当您进入界面时,您可以通过一种快速(且肮脏)的方式进入界面的主要实现。F12->F12!