C # hide from dervied classes some properties of a base class

I want to know if a property of a base class can be hidden from a derived class:

Example:

    class BaseDocument
    {
        public string DocPath{get; set;}
        public string DocContent{get; set;}
    } 

    class DerviedDocument: BaseDocument
    {
     //this class should not get the DocContent property
        public Test()
        {
           DerivedDocument d = new DerivedDocument();
           d.//intellisense should only show me DocPath
             //I do not want this class to see the DocContent property
        }
    }

I cannot make the DocContent property private, because I want to create an instance of the BaseDocument class elsewhere and use the property there. This will kill the idea of ​​ownership anyway.

One way to fix this would be to use an interface, such as an IDoc, that exposes the DocPath property and allows you to implement the BaseDocument and DerivedDocument interfaces. This will break the relationship between parents and children.

I can play with new and redefining keywords, but this is not so, because the child still β€œsees” the property

"" DocContent, .

, , , , , .

?

0
7

, - . , , EditorBrowsableAttribute, , . :

public interface IDoc
{
   DocPath{get;set;}
}

class BaseDocument : IDoc
{
     public DocPath{get; set;}
     public DocContent{get; set;}
} 

class DerviedDocument
{
    public DerivedDocument(IDoc doc)
    {
        this.Doc = doc;
    }

    public IDoc Doc{get;set;}

     public Test()
     {
        DerivedDocument d = new DerivedDocument(new BaseDocument());
        d.//here you will only see d.IDoc which only exposes DocPath

     }
}

, , , .

+3

, BaseDocument DerivedDocument /.

DocContent . , BaseDocument, DerivedDocument, . , BaseDocument ( , ).

:

public class BaseDocument
{
    public string DocPath {get; set;}
    internal string DocContent {get; set;}
}

, :

class DerivedDocument : FirstProject.BaseDocument
{
    public Test()
    {
       DerivedDocument d = new DerivedDocument();
       d.  //intellisense shows DocPath, but not DocContent
    }
}

, . BaseDocument DocContent BaseDocument. DocContent ( DerivedDocument), InternalsVisibleTo, DocContent . ( , , , kludge, .)

+3

, . , ? .

+2
interface IBaseDocument
{
    string DocPath    { get ; set ; }
    string DocContent { get ; set ; }
} 

class BaseDocument : IBaseDocument
{
    public string DocPath { get ; set ; } // implement normally

    private string MyDocContent ;   // use this in BaseDocument
    string IBaseDocument.DocContent // implement explicitly
    { 
        get { return MyDocContent  ; } 
        set { MyDocContent = value ; } 
    }
} 

class DerviedDocument : BaseDocument
{
    public void Test ()
    {
       // error: The name 'DocContent' does not exist in the current context
       Console.WriteLine (DocContent) ; 
    }
}
+1

, ( ) . , , DocContent BaseDocument, BaseDocument, DerivedDocument, , DocContent.

0

, .

: Base DocContent internal public:

class BaseDocument
{
    public string DocPath{get; set;}
    internal string DocContent{get; set;} //won't be visible outside the assembly
}

attributes, :

class BaseDocument
{
    public string DocPath{get; set;}
    public string DocContent{get; set;}
} 

class DerviedDocument: BaseDocument
{
   //this class should not get the DocContent property

  [Browsable(false), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  public new string DocContent{ get; set; }

  public Test()
  {
     DerivedDocument d = new DerivedDocument();
     d.//intellisense will only show me DocPath
     //I do not want this class to see the DocContent property
  }
}
0

.

class BaseDocument
{
    public DocPath{get; set;}
    public virtual DocContent{get; set;}
} 

class DerviedDocument: BaseDocument
{
    public override DocContent 
    { 
        get { return null; }
        set { } 
    }    
}

public override DocContent 
{ 
    get { throw new NotImplementedException("Do not use this property!"); }
    set { throw new NotImplementedException("Do not use this property!"); } 
} 
-1

Source: https://habr.com/ru/post/1762416/


All Articles