本文介绍了如何限制受保护的成员只能从第一级子 C# 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个级别的课程.

I have 3 level classes.

  • TopClass
  • MiddleClass
  • BottomClass

MiddleClass 派生自 TopClassBottomClass 派生自 MiddleClass.

And MiddleClass is derived from TopClass and BottomClass is derived from MiddleClass.

我想在 TopClass 中有一个属性,它只能从 TopClassMiddleClass 而不是 BottomClass 访问>.

I want to have a property in TopClass which could only be accessible from TopClass and MiddleClass not the BottomClass.

当我使用 protected 访问修饰符时,TopClass 中的 protected 属性可以从 BottomClass 访问.

As I am using protected access modifier, the protected property in TopClass is accessible from BottomClass.

这是我的课程,以提供更好的洞察力.

Here are my classes to give better insight.

public class TopClass
 {
    private string ThisIsOnlyAccessibleForTopClass { get; set; } 
    protected string ThisIsOnlyAccessibleForTopClassAndMiddleClass { get; set; } // I want this to be accessible only by the MiddleClass 
    public string AccessibleFromEverywhere { get; set; } // this is good as it is public so accessible everywhere

    public virtual void SomeWeirdFunction()
    {
       ThisIsOnlyAccessibleForTopClass = "I can access this here as it is grand father's private member!"; 
    }
 }


 public class MiddleClass : TopClass
 {
     public override void SomeWeirdFunction()
     {
         base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "As this is a MiddleClass class, I am accessible here.. That is good!";
     }
 }


 public class BottomClass : MiddleClass
 {
     public override void SomeWeirdFunction()
     {
         base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "I don't want this to be accessible here!";
     }
 }

我希望 ThisIsOnlyAccessibleForTopClassAndMiddleClass 只能从 MiddleClassTopClass 访问,而不是从 BottomClass 访问.

I want ThisIsOnlyAccessibleForTopClassAndMiddleClass to be only accessible from the MiddleClass and TopClass, not from BottomClass.

我该怎么做?因为我无法使用现有的访问修饰符(例如 protectedpublicinternalprivate 等)来做到这一点.这是否表明我试图在这里打破某种规则?

How can I do that? And as I cannot do this with existing access modifiers such as protected, public, internal , private etc.. is it a sign that I am trying to break some sort of rule here?

推荐答案

如果您有一个特定 MiddleClass 应该对 TopClass 的成员具有特权访问权限,您可以将 MiddleClass 作为嵌套类型放在 TopClass 中.嵌套类型可以访问其封闭类型的所有成员.

If you have a specific MiddleClass that should have privileged access to a member of TopClass, you can put MiddleClass in TopClass as a nested type. Nested types have access to all members of their enclosing type.

public class TopClass
{
    private string ThisIsOnlyAccessibleForTopClass { get; set; } 
    private string ThisIsOnlyAccessibleForTopClassAndMiddleClass { get; set; } // I want this to be accessible only by the MiddleClass 
    public string AccessibleFromEverywhere { get; set; } // this is good as it is public so accessible everywhere

    public virtual void SomeWeirdFunction()
    {
        ThisIsOnlyAccessibleForTopClass = "I can access this here as it is grand father's private member!"; 
    }

    public class MiddleClass : TopClass
    {
        public override void SomeWeirdFunction()
        {
            base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "As this is a MiddleClass class, I am accessible here.. That is good!";
        }
    }

}

public class BottomClass : TopClass.MiddleClass
{
    public override void SomeWeirdFunction()
    {
        // compilation error:
        // base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "I don't want this to be accessible here!";
    }
}

如果您在与 TopClass 相同的程序集中有一个或多个 MiddleClass 类,并且所有 BottomClass 类都将在另一个程序集中程序集,您可以使用private protected.它的意思是只访问 internalprotected 的类",而 protected internal 的意思是访问 EITHER 的类"内部protected".

If you have one or more MiddleClass classes in the same assembly as TopClass, and all the BottomClass classes are going to be in another assembly, you can use private protected. It means "access to only classes that are BOTH internal and protected", whereas protected internal means "access to classes that are EITHER internal or protected".

// in Assembly A

public class TopClass
{
    private string ThisIsOnlyAccessibleForTopClass { get; set; } 
    private protected string ThisIsOnlyAccessibleForTopClassAndMiddleClass { get; set; } // I want this to be accessible only by the MiddleClass 
    public string AccessibleFromEverywhere { get; set; } // this is good as it is public so accessible everywhere

    public virtual void SomeWeirdFunction()
    {
        ThisIsOnlyAccessibleForTopClass = "I can access this here as it is grand father's private member!"; 
    }
}

public class MiddleClass : TopClass
{
    public override void SomeWeirdFunction()
    {
        base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "As this is a MiddleClass class, I am accessible here.. That is good!";
    }
}

// in Assembly B, which references Assembly A

public class BottomClass : MiddleClass
{
    public override void SomeWeirdFunction()
    {
        // compile error
        // base.ThisIsOnlyAccessibleForTopClassAndMiddleClass = "I don't want this to be accessible here!";
    }
}

这篇关于如何限制受保护的成员只能从第一级子 C# 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:11