简体   繁体   English

为什么C#禁止对接口成员进行内部访问?

[英]Why does C# prohibit internal access on interface members?

C# deliberately prevents me from putting the internal access modifier on interface members. C#故意阻止我将内部访问修饰符放在接口成员上。 I'm pretty sure they had to put additional effort into implementing this restriction when they could have allowed it just as well. 我很确定他们在允许的情况下也必须付出更多的努力来实施此限制。

There should be a good reason to spend that effort - which one? 应该有充分的理由来花费这一努力-哪一个?

For me it would be useful to have internal access modifiers on interfaces to realize "mutability prevention from the outside". 对我来说,在接口上具有内部访问修饰符以实现“防止外部变异”将是有用的。 Eg consider the following interface: 例如考虑以下接口:

interface IMessage
{
    ...
    DateTime LastSentTimestamp { get; internal set; }
    ...
}

Internally, within my message broker, I would like to be able to set the LastSentTimestamp . 在内部,在我的消息代理中,我希望能够设置LastSentTimestamp External users of my API should not be able to change it. 我的API的外部用户不能更改它。 Of course I could ditch the interface and use a class instead, however, this removes all the pros of interfaces (eg change implementation anytime, etc.) 当然,我可以抛弃接口并改用类,但是,这消除了接口的所有优点(例如,随时更改实现等)。

Q1: I don't get it why they restrict me. Q1:我不明白为什么他们限制了我。 Is there a particular reason? 有什么原因吗?

Q2: I'm looking for a workaround for my particular situation above. Q2:我正在寻找一种针对上述情况的解决方法。 Splitting the interface in two ( IMessage and IInternalMessage ) looks like a big pain in the ass and is hopefully not my only option left... 将接口拆分为两个( IMessageIInternalMessage )看起来很IInternalMessage ,希望这不是我剩下的唯一选择...

I guess the conflict that the C# designers have seen was that you cannot implement an interface with an internal member outside of the declaring assembly (and friend assemblies). 我猜想C#设计人员已经看到的冲突是,您不能与声明程序集(和友元程序集)外部的internal成员实现接口。 Of course, it could still be useful to do this if outside assemblies only consume the interface. 当然,如果外部程序集仅使用该接口,则这样做仍然有用。 On the other hand you can get this behavior with a second internal -only interface: 另一方面,您可以通过第二个internal -only接口获得此行为:

public interface IPublic { /* all public members */ }
internal interface IPrivate : IPublic { /* additional members */ }

And your own classes implement IPrivate . 您自己的类实现IPrivate External code can only use IPublic . 外部代码只能使用IPublic

It's not your interfaces' members which should have access modifiers but the overall interface, so as to have consistent accessibility, and your classes. 具有一致的可访问性的,不是整个接口的成员而是应该具有访问修饰符的接口成员,以及您的类。 Interfaces only get used when implemented by a class. 接口仅在由类实现时使用。 Interfaces express a contract which must be realized by an implementer. 接口表示必须由实施者实现的合同。 How would a class implement your internal member if it lived outside the interface's assembly? 如果某个类的内部成员不在接口的程序集中,则该类将如何实现它? Thereby it is unable to implement the contract, but classes within must be able to...that's logically inconsistent. 因此,它无法实现合同,但是其中的类必须能够...在逻辑上是不一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM