为什么内部保护不比内部更严格?

发布于 2024-07-25 18:02:18 字数 316 浏览 5 评论 0 原文

我想创建一个内部自动属性:

internal bool IP { get; protected internal set; }

我认为可以使设置器受保护受保护内部 - 但我总是收到错误可访问性修饰符必须比属性更具限制性。 不是这样的吗? Private 在这里对我没有帮助。

编辑:
问题是:如何实现具有内部 getter 和受保护 setter 的自动属性?

I'd like to create an internal auto-property:

internal bool IP { get; protected internal set; }

I thought it would be possible to make the setter protected or protected internal - but I always get the error accessibility modifier must be more restrictive than the property. Isn't that the case? Private does not help me, here.

EDIT:
The question is: How do I implement an auto-property with a internal getter and a protected setter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

ペ泪落弦音 2024-08-01 18:02:19

我会认为这是作弊,因为埃里克·利珀特(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.

云淡月浅 2024-08-01 18:02:19

protected insideprotectedinternal 的限制要少,因为它允许其子类 (protected) 和任何相同的程序集(内部)来访问某些内容。

protected internal is less restrictive than either protected or internal because it allows both its subclasses (protected) and anything in the same assembly (internal) to access something.

场罚期间 2024-08-01 18:02:19

受保护的内部意味着对同一程序集中的类或从包含类派生的类可见 - 换句话说,它对那些满足内部要求或受保护要求的人可见,而不是“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.

白衬杉格子梦 2024-08-01 18:02:19

受保护的内部意味着受保护或内部,不受保护和内部。 因此范围仅限于相同的程序集或派生类,而不一定是两者。

protected internal means protected OR internal, not protected and internal. So scope is limited to the same assembly OR derived classes, not necessarily both.

大海や 2024-08-01 18:02:19

辅助功能修饰符必须比属性更具限制性

Internal 比protected 属性更具限制性:因为受保护的内容可以在程序集外部(通过子类)看到。

编译器表示,当整个 IP 属性是内部的(即在程序集外部不可见)时,说 set 受到保护(即对程序集外部的子类可见)是没有意义的。集会)。

accessibility modifier must be more restrictive than the property

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 whole IP property is internal (i.e. invisible outside the assembly).

夕嗳→ 2024-08-01 18:02:18

它受到有效的保护内部,而不是。 同一程序集中的派生类和类型可以两者访问它。 认为受保护的内部意味着只能由同一程序集中的派生类访问是一种常见的误解。

It's effectively protected or internal, not and. It's accessible both by derived classes and types in the same assembly. It's a common misconception to think protected internal means accessible only to derived classes in the same assembly.

旧街凉风 2024-08-01 18:02:18

在 .NET 级别,有两个相似但不同的访问级别:

  • FamilyAndAssembly:比 protected 或内部限制更严格
  • FamilyOrAssembly:比 protected 或内部限制更少

“受保护的内部”在 C# 中意味着 FamilyOrAssembly; FamilyAndAssembly 没有修饰符。

因此,您的 protected inside setter 比 internal 整体属性的限制要少。 你可以做的是:

protected internal bool IP { internal get; set; }

但是你的setter比你的getter受到的限制更少,这很奇怪......

另一个(有点等效)的替代方案是:

internal bool IP { get; set; }

protected void SetIP(bool ip)
{
    this.IP = ip;
}

At the .NET level, there are two similar but distinct access levels:

  • FamilyAndAssembly: more restrictive than either protected or internal
  • FamilyOrAssembly: less restrictive than either protected or internal

"protected internal" in C# means FamilyOrAssembly; there's no modifier for FamilyAndAssembly.

So, your protected internal setter is less restrictive than the internal overall property. What you could do is:

protected internal bool IP { internal get; set; }

But then your setter is less restricted than your getter, which is odd...

Another (somewhat equivalent) alternative is:

internal bool IP { get; set; }

protected void SetIP(bool ip)
{
    this.IP = ip;
}
撩动你心 2024-08-01 18:02:18

考虑到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; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文