How to hide an inherited property in a class without modifying the inherited class (base class)?

前端 未结 10 817
无人共我
无人共我 2020-11-27 20:21

If i have the following code example:

public class ClassBase
{
    public int ID { get; set; }

    public string Name { get; set; }
}

public class ClassA :         


        
相关标签:
10条回答
  • 2020-11-27 20:25

    Just hide it

     public class ClassBase
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class ClassA : ClassBase
    {
        public int JustNumber { get; set; }
        private new string Name { get { return base.Name; } set { base.Name = value; } }
        public ClassA()
        {
            this.ID = 0;
            this.Name = string.Empty;
            this.JustNumber = 0;
        }
    }
    

    Note: Name will still be a public member of ClassBase, given the constraint of not changing the base class there is no way to stop that.

    0 讨论(0)
  • 2020-11-27 20:25

    You can use Browsable(false)

    [Browsable( false )]
    public override string Name
    {
        get { return base.Name; }
        set { base.Name= value; }
    }
    
    0 讨论(0)
  • 2020-11-27 20:26

    Why force inheritance when it's not necessary? I think the proper way of doing it is by doing has-a instead of a is-a.

    public class ClassBase
    {
        public int ID { get; set; }
    
        public string Name { get; set; }
    }
    
    public class ClassA
    {
        private ClassBase _base;
    
        public int ID { get { return this._base.ID; } }
    
        public string JustNumber { get; set; }
    
        public ClassA()
        {
            this._base = new ClassBase();
            this._base.ID = 0;
            this._base.Name = string.Empty;
            this.JustNumber = string.Empty;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 20:27

    I don’t think a lot of the people replying here understand inheritance at all. There is a need to inherit from a base class and hide its once public var’s and functions. Example, lets say you have a basic engine and you want to make a new engine that is supercharged. Well, 99% of the engine you will use but you will tweak a bit of its functionality to make it run much better and yet still there is some functionality that should only be shown to the modifications made, not the end user. Because we all know that every class MS puts out doesn’t really ever need any modifications.

    Besides using the new to simply override the functionality it is one of the things that Microsoft in their infinite wis….. oh, I mean mistakes considered a tool not worthwhile anymore.

    The best way to accomplish this now is multi-level inheritance.

    public class classA 
    {
    }
    
    public class B : A 
    {} 
    
    public class C : B 
    {} 
    

    Class B does all your work and class C exposes what you need exposed.

    0 讨论(0)
  • 2020-11-27 20:28

    While technically the property won't be hidden, one way to strongly discourage its use is to put attributes on it like these:

    [Browsable(false)]
    [Bindable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    

    This is what System.Windows.Forms does for controls that have properties that don't fit. The Text property, for instance, is on Control, but it doesn't make sense on every class that inherits from Control. So in MonthCalendar, for instance, the Text property appears like this (per the online reference source):

    [Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never),
        Bindable(false), 
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string Text {
        get { return base.Text; }
        set { base.Text = value; }
    }
    
    • Browsable - whether the member shows up in the Properties window
    • EditorBrowsable - whether the member shows up in the Intellisense dropdown

    EditorBrowsable(false) won't prevent you from typing the property, and if you use the property, your project will still compile. But since the property doesn't appear in Intellisense, it won't be as obvious that you can use it.

    0 讨论(0)
  • 2020-11-27 20:29

    I know that the question is old, but what you can do is override the PostFilterProperties like this:

     protected override void PostFilterProperties(System.Collections.IDictionary properties)
        {
            properties.Remove("AccessibleDescription");
            properties.Remove("AccessibleName");
            properties.Remove("AccessibleRole");
            properties.Remove("BackgroundImage");
            properties.Remove("BackgroundImageLayout");
            properties.Remove("BorderStyle");
            properties.Remove("Cursor");
            properties.Remove("RightToLeft");
            properties.Remove("UseWaitCursor");
            properties.Remove("AllowDrop");
            properties.Remove("AutoValidate");
            properties.Remove("ContextMenuStrip");
            properties.Remove("Enabled");
            properties.Remove("ImeMode");
            //properties.Remove("TabIndex"); // Don't remove this one or the designer will break
            properties.Remove("TabStop");
            //properties.Remove("Visible");
            properties.Remove("ApplicationSettings");
            properties.Remove("DataBindings");
            properties.Remove("Tag");
            properties.Remove("GenerateMember");
            properties.Remove("Locked");
            //properties.Remove("Modifiers");
            properties.Remove("CausesValidation");
            properties.Remove("Anchor");
            properties.Remove("AutoSize");
            properties.Remove("AutoSizeMode");
            //properties.Remove("Location");
            properties.Remove("Dock");
            properties.Remove("Margin");
            properties.Remove("MaximumSize");
            properties.Remove("MinimumSize");
            properties.Remove("Padding");
            //properties.Remove("Size");
            properties.Remove("DockPadding");
            properties.Remove("AutoScrollMargin");
            properties.Remove("AutoScrollMinSize");
            properties.Remove("AutoScroll");
            properties.Remove("ForeColor");
            //properties.Remove("BackColor");
            properties.Remove("Text");
            //properties.Remove("Font");
        }
    
    0 讨论(0)
提交回复
热议问题