C# 自动生成基类方法的实现

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

Automatically generate implementations of base class methods

c#visual-studio

提问by DancesWithBamboo

Is there a shortcut of some kind in C# (VS 2008) to automatically implement the virtual and abstract base class methods in a derived class?

C#(VS 2008)中是否有某种快捷方式可以在派生类中自动实现虚拟和抽象基类方法?

采纳答案by Jeff Yates

For virtual methods, you can type overrideand then a space. Intellisense should offer you a list of options.

对于虚方法,您可以先输入override一个空格。Intellisense 应该为您提供一个选项列表。

For abstract methods and properties, you can use the smart tag on the base class or interface (also, Ctrl+.or Shift+Alt+F10will show the smart tag menu) to generate the concrete items.

对于抽象方法和属性,您可以使用基类或接口的智能标签(也,Ctrl+.Shift+ Alt+F10将显示智能标记菜单),以产生具体项目。

For example, in the following code snippet, you could place the caret at the end of INotifyPropertyChangedand press Ctrl+.to then select Implement Interface, and the PropertyChangedevent would be added to MyClass:

例如,在下面的代码片段中,您可以将插入符号放在末尾INotifyPropertyChanged并按Ctrl+.选择Implement Interface,PropertyChanged事件将被添加到MyClass

class MyClass : INotifyPropertyChanged
{
}

回答by eKek0

For virtual methods type override, give an space and intellisense will show you all methods that can be inherited.

对于虚拟方法类型override,给一个空格,intellisense 将显示所有可以继承的方法。

回答by CMS

Just type the Interface that you want to implement, and then click on the Smart Tag, a context menu will popup, and then you can select either Implement Interface or Implement Interface Explicitly:

只需输入您要实现的接口,然后单击智能标记,将弹出一个上下文菜单,然后您可以选择实现接口或显式实现接口:

enter image description here

在此处输入图片说明

All the members to be overridden will be contained within a code region that is named to reflect its purpose.

要覆盖的所有成员都将包含在命名以反映其目的的代码区域中。

All the members will have a line that throws a NotImplementedException.

所有成员都会有一行抛出NotImplementedException.

回答by viggity

As the others have mentioned, the smart tag (via Ctrl+.and Alt+Shft+F10) will solve your problem, however Visual Studio has an annoying option that will automatically wrap the generated properties/methods with a region like this:

正如其他人所提到的,智能标记(viaCtrl+.Alt+Shft+F10)将解决您的问题,但是 Visual Studio 有一个烦人的选项,它会自动将生成的属性/方法包装在这样的区域中:

#region INotifyPropertyChanged members
...
#endregion

You can turn this functionality by going to Tools > Options > Text Editor > C# > Advanced. There is a checkbox called Implement Interface > Surround generated code with #region. Just make sure it is unchecked.

您可以通过转到 来启用此功能Tools > Options > Text Editor > C# > Advanced。有一个名为 的复选框Implement Interface > Surround generated code with #region。只要确保它没有被选中。

回答by John

Don't think this existed when the original question was asked, but at least as of VS 2013, you can automatically create stubs for abstract methods & properties. Just right click on the abstract class name (in your class definition) and pick "Implement Abstract Class". Just like CMS showed with automatically implementing interfaces.

不要认为在提出原始问题时存在这种情况,但至少从 VS 2013 开始,您可以自动为抽象方法和属性创建存根。只需右键单击抽象类名称(在您的类定义中)并选择“实现抽象类”。就像 CMS 显示的自动实现接口一样。

回答by Chris Halcrow

The current official Microsoft documentation for automatically implementing an abstract base class is here:

自动实现抽象基类的当前微软官方文档在这里:

https://docs.microsoft.com/en-us/visualstudio/ide/reference/implement-abstract-class

https://docs.microsoft.com/en-us/visualstudio/ide/reference/implement-abstract-class

回答by Sam Tigle

Maybe you want all inheriting/implementing classes to implement a new defined abstract method.

也许您希望所有继承/实现类都实现一个新定义的抽象方法。

  1. Go to one of the inheriting/implementing classes
  2. use smart tag menu ctrl+.
  3. implement abstract class / interface
  4. watch for options at the bottom of the popup window "Project" or "Solution" Smart tag Menu Pop Up
  1. 转到继承/实现类之一
  2. 使用智能标签菜单ctrl+.
  3. 实现抽象类/接口
  4. 注意弹出窗口“项目”或“解决方案”底部的选项 智能标签菜单弹出