为什么内部保护不比内部更严格?
我想创建一个内部自动属性:
internal bool IP { get; protected internal set; }
我认为可以使设置器受保护
或受保护内部
- 但我总是收到错误可访问性修饰符必须比属性更具限制性。 不是这样的吗? Private
在这里对我没有帮助。
编辑:
问题是:如何实现具有内部 getter 和受保护 setter 的自动属性?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会认为这是作弊,因为埃里克·利珀特(Eric Lippert)本人就是这样,但他写了一篇很棒的博客文章来考虑这个问题。
为什么我不能从派生类访问受保护的成员,第三部分
最终,他的答案与此处的海报给出的基本相同,但他提出了一些有趣的推理语言的设计和这些功能的实现背后。
I would consider this cheating, since Eric Lippert is on SO himself, but he wrote an excellent blog post that considers this issue.
Why Can't I Access A Protected Member From A Derived Class, Part Three
Ultimately, his answer is largely the same as those given by the posters here, but he ads some interesting reasoning behind the desgin of the language and the implementation of these features.
protected inside
比protected
或internal
的限制要少,因为它允许其子类 (protected
) 和任何相同的程序集(内部
)来访问某些内容。protected internal
is less restrictive than eitherprotected
orinternal
because it allows both its subclasses (protected
) and anything in the same assembly (internal
) to access something.受保护的内部意味着对同一程序集中的类或从包含类派生的类可见 - 换句话说,它对那些满足内部要求或受保护要求的人可见,而不是“AND”。 没有访问修饰符意味着以这种方式受保护和内部。
protected internal means visible to classes in the same assembly, or to classes deriving from the containing class - in other words it is visible to those meeting the internal requirements OR the protected requirements, not AND. There is no access modifier meaning protected AND internal in this way.
受保护的内部意味着受保护或内部,不受保护和内部。 因此范围仅限于相同的程序集或派生类,而不一定是两者。
protected internal means protected OR internal, not protected and internal. So scope is limited to the same assembly OR derived classes, not necessarily both.
Internal 比protected 属性更具限制性:因为受保护的内容可以在程序集外部(通过子类)看到。
编译器表示,当整个
IP
属性是内部的(即在程序集外部不可见)时,说set
受到保护(即对程序集外部的子类可见)是没有意义的。集会)。Internal is more restrictive that protected: because protected things can be seen (by subclasses) outside the assembly.
The compiler is saying that there's no sense in saying that
set
is protected (i.e. visible to subclasses outside the assembly), when the wholeIP
property is internal (i.e. invisible outside the assembly).它受到有效的
保护
或内部
,而不是和。 同一程序集中的派生类和类型可以两者访问它。 认为受保护的内部意味着只能由同一程序集中的派生类访问是一种常见的误解。It's effectively
protected
orinternal
, not and. It's accessible both by derived classes and types in the same assembly. It's a common misconception to thinkprotected internal
means accessible only to derived classes in the same assembly.在 .NET 级别,有两个相似但不同的访问级别:
“受保护的内部”在 C# 中意味着 FamilyOrAssembly; FamilyAndAssembly 没有修饰符。
因此,您的
protected inside
setter 比internal
整体属性的限制要少。 你可以做的是:但是你的setter比你的getter受到的限制更少,这很奇怪......
另一个(有点等效)的替代方案是:
At the .NET level, there are two similar but distinct access levels:
"protected internal" in C# means FamilyOrAssembly; there's no modifier for FamilyAndAssembly.
So, your
protected internal
setter is less restrictive than theinternal
overall property. What you could do is:But then your setter is less restricted than your getter, which is odd...
Another (somewhat equivalent) alternative is:
考虑到Jon Skeet提到的(以及用户59808的评论),这不会达到预期的结果吗?
受保护的内部 bool IP { get; 受保护集; }
Considering what Jon Skeet mentioned (and user59808's comment), wouldn't this achieve the desired result?
protected internal bool IP { get; protected set; }