属性的访问修饰符;为什么以下不起作用?

发布于 2024-10-07 20:24:50 字数 398 浏览 6 评论 0原文

我遇到了一个编译器错误,这对我来说不太有意义。我有一个 internal 属性,我想限制它的 set 块,使其只能通过继承使用。我认为这会起作用:

internal bool MyProperty {
    get { return someValue; }
    protected internal set { someValue = value; }
}

但编译器说 set 块上的访问修饰符需要比 internal 更具限制性 - 我是否遗漏了某些内容,或者是 protected insideinternal限制更多吗?

I've run into a compiler error that doesn't quite make sense to me. I have an internal property and I want to restrict its set block such that it is only available through inheritance. I thought this would work:

internal bool MyProperty {
    get { return someValue; }
    protected internal set { someValue = value; }
}

But the compiler says that the access modifier on the set block needs to be more restrictive than internal - am I missing something, or is protected internal not more restrictive than internal?

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

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

发布评论

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

评论(4

孤单情人 2024-10-14 20:24:50

受保护的内部限制较少;它是受保护的内部(不是) - 因此另外允许来自其他程序集的子类访问它。您需要反转:

protected internal bool MyProperty {
    get { return someValue; }
    internal set { someValue = value; }
}

这将允许程序集中的代码以及其他程序集中的子类获取它(读取) - 但只有程序集中的代码可以设置它(写)。

protected internal is less restrictive; it is protected or internal (not and) - which therefore additionally allows subclasses from other assemblies to access it. You would need to invert:

protected internal bool MyProperty {
    get { return someValue; }
    internal set { someValue = value; }
}

This will allow code in your assembly, plus subclasses from other assemblies, get it (read) - but only code in your assembly can set it (write).

各自安好 2024-10-14 20:24:50

来自 C# 中访问修饰符的文档

受保护的内部可访问性
level 表示受保护或内部,而不是
受保护且内部。
在其他情况下
换句话说,受保护的内部成员可以
可以从同一类中的任何类访问
程序集,包括派生类。
将可访问性限制为仅派生
同一程序集中的类,声明
类本身是内部的,并声明
其成员受到保护。

为了达到预期的效果,您需要交换访问修饰符,如下所示:

protected internal bool MyProperty
{
    get { return someValue; }
    internal set { someValue = value; }
}

From the documentation on Access Modifiers in C#:

The protected internal accessibility
level means protected OR internal, not
protected AND internal.
In other
words, a protected internal member can
be accessed from any class in the same
assembly, including derived classes.
To limit accessibility to only derived
classes in the same assembly, declare
the class itself internal, and declare
its members as protected.

To achieve the desired effect, you instead need to swap the access modifiers, like so:

protected internal bool MyProperty
{
    get { return someValue; }
    internal set { someValue = value; }
}
寻梦旅人 2024-10-14 20:24:50

不,这是两者的并集,而不是交集;因此,受保护的内部限制比这两个单独的限制要少。交集不是 C# 的功能; CLR 确实支持“Family AND Assembly”,但 C# 只支持“Family OR Assembly”。

No, it's the union of the two, not the intersection; hence protected internal is less restrictive than both of those individually. The intersection isn't a feature of C#; the CLR does support "Family AND Assembly", but C# only supports "Family OR Assembly".

罗罗贝儿 2024-10-14 20:24:50

在这里,受保护的内部内部限制更少。

  • 受保护的内部 - 当前程序集以及在其他程序集中继承此类型的任何类型的公共。

  • internal - 此程序集公共,其他程序集私有

Here, protected internal is less restrictive that internal.

  • protected internal - public for current assembly and any type that inherits this type in other assemblies.

  • internal - public for this assembly and private for other assemblies

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