C# 的“受保护的内部”意思是“受保护的”。 *或*“内部”。任何关键字是否意味着“受保护”? *和*“内部”?

发布于 2024-10-31 14:30:55 字数 99 浏览 3 评论 0 原文

我需要声明一个既受保护又受内部保护的成员。然而,令我完全困惑的是,我刚刚发现“受保护的内部”实际上意味着“受保护的或内部的”。是否有任何访问修饰符表示受保护的内部?

I need to declare a member that is both protected AND internal. However, to my complete bafflement, I just discovered the hard way that "protected internal" actually means protected OR internal. Is there any access modifier that means protected AND internal?

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

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

发布评论

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

评论(5

心清如水 2024-11-07 14:30:55

虽然 CLR 支持它,但在 C# 中没有强制成员受到保护和内部的方式。

C# 和 VB.NET 都使用并集而不是交集来组合访问修饰符。

如果您确实需要的话,有一个解决方法。它不干净,但它有效。您可以创建一个带有内部属性的辅助类,然后将该内部类类型的受保护属性添加到您的类中。受保护属性的内部属性只能在所属程序集中的子类上访问。

示例如下。我使用泛型是因为您可能需要不同类型的多个受保护的内部属性。泛型将允许您使用一个内部类,而不管所需的属性类型如何。

public class AccessHelper<T>
{
    internal T Value { get; set; }
}

public class AClass
{
    public AClass()
    {
        InternalProperty.Value = "Can't get or set this unless you're a derived class inside this assembly.";
    }

    protected AccessHelper<String> InternalProperty
    {
        get;
        set;
    }
}

Though the CLR supports it, in C# there is no way to force a member to be protected AND internal.

Both C# and VB.NET combine access modifiers using a union, rather than intersection.

There is a workaround for this if you absolutely have to have it. It's not clean, but it works. You can create a helper class with an internal property on it, and then add a protected property of that inner class type to your class. The internal property of the protected property will only be accessible on a subclass within the owning assembly.

Example follows. I've used a generic on the chance that you might want multiple protected internal properties of different types. The generic will allow you to use the one inner class regardless of the desired property type.

public class AccessHelper<T>
{
    internal T Value { get; set; }
}

public class AClass
{
    public AClass()
    {
        InternalProperty.Value = "Can't get or set this unless you're a derived class inside this assembly.";
    }

    protected AccessHelper<String> InternalProperty
    {
        get;
        set;
    }
}
虚拟世界 2024-11-07 14:30:55

protected AND 内部在 C#(或任何其他高级 .NET 语言据我所知)中不可用。虽然它是由CLR支持的并且可以在IL中实现。

protected AND internal is not available in C# (or any other high level .NET language afaik). Although it is supported by the CLR and can be achieved in IL.

緦唸λ蓇 2024-11-07 14:30:55

引用 Equiso 的链接:

顺便说一句,CLR 确实有 ProtectedANDInternal 的概念,但 C# 没有语法来指定它。如果您查看 CLR 的 System.Reflection.MethodAttributes 枚举,您将看到 FamANDAssem 和 FamORAssem(“Family”是 CLR 中 C# 受保护的术语,“Assem”是 C# 内部的)。

Citing from Equiso's link:

BTW the CLR does have the notion of ProtectedANDInternal, but C# has no syntax to specify it. If you look at the CLR’s System.Reflection.MethodAttributes enum you’ll see both FamANDAssem as well as FamORAssem (“Family” is the CLR’s term for C#’s protected and “Assem” is C#’s internal).

放肆 2024-11-07 14:30:55

You can't define protected AND internal members in C#, although it is supported by the CLR (MemberAttributes.FamilyAndAssembly)

九厘米的零° 2024-11-07 14:30:55

内部类和受保护成员怎么样?

How about internal class and protected member.

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