您如何在C#中自动实现接口? [英] How do you auto-implement an interface in C#?

查看:545
本文介绍了您如何在C#中自动实现接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,在Visual Studio中,如果使用接口声明了一个类,则可以将光标放在该接口上,右键单击它,然后选择实现接口. (请参阅此处针对VS 2008的类似问题.)

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 here for a similar question for VS 2008.)

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

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 Interface menu option is gone.

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

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

很抱歉,如果重复的话,我在任何地方都找不到这个答案.

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

编辑记录-2018年8月17日

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

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.

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

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 public accessor 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.

推荐答案

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

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.

这篇关于您如何在C#中自动实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆