你如何在 C# 中自动实现一个接口?

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

How do you auto-implement an interface in C#?

c#visual-studiointellisense

提问by RLH

Previously in Visual Studio, if you declared a class with an interface you could put your cursor on the interface, right-click it and select Implement Interface. (See herefor a similar question for VS 2008.)

以前在 Visual Studio 中,如果您声明了一个带有接口的类,您可以将光标放在该接口上,右键单击它并选择“实现接口”。(有关VS 2008 的类似问题,请参见此处。)

I've started working on a project that has defined numerous interfaces that will be used with SimpleInjector. Now, as I am beginning to write my service classes, I've noticed that the Implement Interfacemenu option is gone.

我已经开始着手一个项目,该项目定义了许多将与 SimpleInjector 一起使用的接口。现在,当我开始编写服务类时,我注意到“实现接口”菜单选项消失了。

How can I implement all necessary method stubs like I could in VS 2008?

如何像在 VS 2008 中那样实现所有必要的方法存根?

Sorry if this is a duplicate, however, I couldn't find this answer anywhere.

对不起,如果这是重复的,但是,我在任何地方都找不到这个答案。



EDIT NOTES - Aug 17, 2018

编辑说明 - 2018 年 8 月 17 日

This has been a hot question over the years. I've updated the title and tags for this so that it covers all pertinent versions of C# and Visual Studio.

多年来,这是一个热门问题。我已经为此更新了标题和标签,以便它涵盖 C# 和 Visual Studio 的所有相关版本。

This was ultimately a coder FAIL on my part. Much like classes, interfaces when generated by VS aren't defined as public. You will have to manually modify the interface declaration with the publicaccessor since VS doesn't automatically add it. It's easy to forget this step because you don't have to set member modifiers within the interface since, by definition, they will all be public.

这最终是我的编码器失败。与类非常相似,由 VS 生成的接口不定义为public. 您必须使用访问器手动修改接口声明,public因为 VS 不会自动添加它。很容易忘记这一步,因为您不必在接口中设置成员修饰符,因为根据定义,它们都是public.

采纳答案by Cam Bruce

That hasn't changed. All you need to do is hover over the interface name in the class, and the blue underline bar should show up, giving you options to implement the interface. If the blue underline is not appearing, check to make sure the interface is accessible with what assemblies and namespaces you are referencing.

那没有改变。您需要做的就是将鼠标悬停在类中的接口名称上,蓝色下划线应该会出现,为您提供实现接口的选项。如果没有出现蓝色下划线,请检查以确保可以使用您引用的程序集和命名空间访问该接口。

回答by Curtis Rutland

When you look at the interface name, you should see a little blue underline:

当您查看接口名称时,您应该会看到一个蓝色的小下划线:

first image

第一张图片

If you hover over it, a drop down menu will appear, letting you implement the interface:

如果你将鼠标悬停在它上面,会出现一个下拉菜单,让你实现接口:

second image

第二张图片

回答by Sinaesthetic

Put your cursor somewhere in the interface text (the part after the colon) and hit Ctrl + .

将光标放在界面文本中的某个位置(冒号后面的部分)并点击 Ctrl + .