为什么要实施方法内部接口是公开的 [英] Why must methods implementing internal interfaces be public

查看:116
本文介绍了为什么要实施方法内部接口是公开的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是开发可实现内部接口的内部类。
任何人都可以解释为什么我不能宣布我的方法是内部的,为什么我收到以下错误:无法实现接口成员,因为它是不公开

I am developing an internal class that implements an internal interface. Can anyone explain why I cannot declare my method as internal, why I am getting the following error: "cannot implement an interface member because it is not public".

我知道,我必须声明的方法为public,但它是完全没有意义的我。

I know that I have to declare the method as public, but it makes absolutely no sense to me.

这是声明的方法公开如果两个接口和类是内部的点?
是难道不是误导?

What is the point of declaring a method public if both the interface and the class are internal? Is it not misleading?

我看过的这个网站一个相关的问题。它不是一个确切的重复,因为我的课是内部

I have read a related question on this site. It is not an exact duplicate, because my class is internal.

推荐答案

简单地说:因为这是语言的设计者设计的方式。甚至在内部接口,该方法是隐式公开的。它确实让事情变得简单,但它在其他方面有疼痛感。

Simply put: because that's the way the language designers designed it. Even in internal interfaces, the methods are implicitly public. It does make things simple, but it's a pain in other ways.

如果你想你想要一个公共类隐藏使用内部接口,你可以使用显式接口实现 - 虽然有其他缺点

If you want a public class where you want to "hide" the use of an internal interface, you could use explicit interface implementation - although that has other drawbacks.

当然,如果你的类是内部的话也没关系,这些方法都是公共反正 - 其他组件不会能够调用的方法,因为他们看不到的类型。

Of course, if your class is internal then it doesn't matter that the methods are public anyway - other assemblies aren't going to be able to call the methods because they can't see the type.

我绝对同意C#(或一般.NET)还没有。被那样小心,因为它可能是围绕内部接口设计

I definitely agree that C# (or .NET in general) hasn't been designed as carefully as it might be around internal interfaces.

在方面的究竟的为什么你会得到一个错误的信息 - 13.4节。在C#4规格(接口映射)的4就是这个道理。实现只发现非静态公共成员和显式接口成员实现 - 如果有在接口的任何未实现的成员,则会出现错误

In terms of exactly why you're getting an error message - section 13.4.4 of the C# 4 spec (interface mapping) is the reason. Implementations are only found for nonstatic public members and explicit interface member implementations - and if there are any unimplemented members in the interface, an error occurs.

这篇关于为什么要实施方法内部接口是公开的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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