C# 从现有类生成接口

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

Generate Interface from existing class

c#visual-studio

提问by Sandy

I have a class as:

我有一个类:

Class MyClass
{
   public MyClass { ... }
   public string Name { get { ... } }
   public int IdNumber { get { ... } set { ... } }
   public void GenerateNme {...}
}

It is just a sample class. I wish to generate Interface from it. Like, MyClass is implementing IMyClass interface. I wish the output to be

它只是一个示例类。我希望从中生成接口。比如,MyClass 正在实现 IMyClass 接口。我希望输出是

public Interface IMyClass
{
   string Name { get; }

   int IdNumber { get; set; }

   void GenerateNumber();
}

and

MyClass : IMyClass
{

}

It can be done manually, but I was just curious to know, is there any other simple method to follow to accomplish this? If not clear, leave a comment.

它可以手动完成,但我只是想知道,有没有其他简单的方法可以做到这一点?如果不清楚,请发表评论。

Thanks.

谢谢。

采纳答案by ken2k

Yes, you can extract an interface from a class using Visual Studio:

是的,您可以使用 Visual Studio 从类中提取接口:

Inside the target class file: Right Click> Refactor> Extract Interface...

在目标类文件中:右键单击>重构>提取接口...

Example

例子

enter image description here

在此处输入图片说明

then

然后

enter image description here

在此处输入图片说明

回答by Ben Robinson

In the refactor menu of visual studio there is an "extract interface" option that does exactly what you describe.

在visual studio的重构菜单中有一个“提取界面”选项,它完全符合你的描述。

回答by Matthias Meid

In Visual Studio 2010, you can right-click MyClassand choose Refactor, `Extract Interface..." (Ctrl+R, I). This gives you a window to check the members to be extracted.

在 Visual Studio 2010 中,您可以右键单击MyClass并选择Refactor, `Extract Interface..." ( Ctrl+ R, I)。这将提供一个窗口来检查要提取的成员。

回答by cdie

In Visual Studio 2015/2017/2019, this is under the Quick Actions menu (Ctrl+ period .)

在 Visual Studio 2015/2017/2019 中,这是在 Quick Actions 菜单下 ( Ctrl+ period .)

Be sure to put the cursor somewhere in the class name you want to extract the interface from. Otherwise it shows "no quick actions available here".

确保将光标放在要从中提取接口的类名中的某个位置。否则它会显示“此处没有可用的快速操作”。

Note: this is only possible if you can actually extract an interface. For example if your class only has static methods this will not work.

注意:这只有在您可以实际提取接口时才有可能。例如,如果您的类只有静态方法,这将不起作用。

回答by Nicholas Petersen

In Visual Studio 2015, click cursor in or right click on the class name, then select Quick Actions (or press Ctrl-.) and the 'Extract Interface' option shows.

在 Visual Studio 2015 中,单击光标或右键单击类名,然后选择“快速操作”(或按Ctrl- .)并显示“提取接口”选项。

回答by Mandeep Janjua

Ctrl+.was popping up 'generating overrides...' and nothing was happening beyond that so I searched 'refactor' in the quick launch search box. Results had Edit -> Refactor -> Extract Interface(Ctrl+R, Ctrl+I) option.

Ctrl+.弹出“生成覆盖...”,除此之外什么也没有发生,所以我在快速启动搜索框中搜索了“重构”。结果有Edit -> Refactor -> Extract Interface( Ctrl+ R, Ctrl+ I) 选项。

Hoping, this tip can help someone else too. I am using VS 2017 EE.

希望这个提示也可以帮助其他人。我正在使用 VS 2017 EE。

回答by silentgut

Please take note. if your class have a "static" in it. you wont be able to extract to interface. so remove that first before you extract to interface

请注意。如果您的班级中有“静态”。您将无法提取到界面。所以在提取到界面之前先删除它

回答by d219

In the latest version of Visual Studio (2019) the menu item has been renamed from the accepted answer. The complete list of how to now get to the interface popup is as follows (only the first listed has changed from earlier versions):

在最新版本的 Visual Studio (2019) 中,菜单项已从接受的答案中重命名。现在如何进入界面弹出窗口的完整列表如下(只有第一个列表与早期版本相比有所变化):

  • Right click on the name of the class and select the Quick Actions and Refactorings...menu option (not Refactor as in earlier versions). Another menu will then appear, select Extract interface....

    Quick Actions and Refactorings popup menu

  • Have the cursor in the name of the class and then hold down Ctrland press .then select the Extract interface...menu option.

    Extract interface popup menu

  • Go to the Editmenu item then Refactorand Extract Interface(you do not need to have the class name selected for this, the operation will be performed on the class you have open)

    Extract interface full menu

  • Hold down Ctrlthen press Rand Iin close succession (again the operation will be performed on the class you have open)

  • 右键单击类的名称并选择Quick Actions and Refactorings...菜单选项(不像早期版本中的 Refactor)。然后会出现另一个菜单,选择Extract interface...

    快速操作和重构弹出菜单

  • 将光标放在类的名称上,然后按住Ctrl并按下,.然后选择Extract interface...菜单选项。

    提取界面弹出菜单

  • 转到Edit菜单项,然后Refactorand Extract Interface(您不需要为此选择类名,操作将在您打开的类上执行)

    提取界面全菜单

  • 按住Ctrl然后连续按RI(再次操作将在您打开的班级上执行)

Performing any of the four above actions will bring up the Extract Interfacewindow (this is largely unchanged from earlier versions, although you can choose to add the interface to your current file)

执行上述四个操作中的任何一个都会打开“提取界面”窗口(这与早期版本基本没有变化,尽管您可以选择将界面添加到当前文件中)

Extract Interface window

提取接口窗口