本文介绍了C#中的"受保护的内部"意味着"保护" *或*"内部&QUOT ;.是否任何关键字意味着"保护" *和*"内部"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明的成员,既保护的的内部。但是,我完全迷惑,我只是发现了艰辛的道路,受保护的内部,其实就是保护或内部。是否有手段加以保护的任何访问修饰符的的内部?

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?

推荐答案

虽然的,在C#是没有办法强迫成员是保护和内部。

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

C#和VB.NET使用相结合的联合,而不是交集访问修饰符。

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;
    }
}

这篇关于C#中的&QUOT;受保护的内部&QUOT;意味着&QUOT;保护&QUOT; *或*&QUOT;内部&QUOT ;.是否任何关键字意味着&QUOT;保护&QUOT; *和*&QUOT;内部&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 02:22