How to hide a base class property correctly

There is a base class (black box, it cannot change it) from which I get. In my derived class, I no longer want to allow users to use the property, instead I have a public function instead. I will not go into details of why (it does not matter), but here is the property that I am trying to hide:

public ComboBoxItemCollection Items { get; } 

I tried this, but this did not work:

 private new ComboBoxItemCollection Items { get; set; } 

I also tried this, however the compiler says that I am not allowed to close both accesses:

 public new ComboBoxItemCollection Items { private get; private set; } 

How to do it right? Please note that I am not dependent on the fact that this is a complete security solution, obviously, thanks to casting the base class, they can still call this property. It simply means offering the user a compile-time error that helps them understand that they cannot use this property and should instead use a new function in the derived class.

EDIT

Thanks to the answers in this thread, I came up with the following solution:

  [Obsolete( "This property is obsolete. Please use MRUIdComboBox.AddItem() instead.", true )] public new ComboBoxItemCollection Items { get { throw new NotSupportedException( "This property is not supported. Please use MRUIdComboBox.AddItem() instead." ); } } 
+4
source share
3 answers

You can not. Closest you could come:

 [Obsolete(IsError=true)] public new ComboBoxItemCollection Items { get { return base.Items; } // Or throw an exception } 

Personally, I will be tempted to ask if you really should flow out of this if you don't want all the members, though ... can you use the composition instead?

+3
source

Even if it was possible, you really do not want to “hide” accessors (as you already noted, you can simply access them through the base class). After a warning / error, you say: "This method is outdated and should be avoided." So you can see the Deprecated Attribute , documented as “The Deprecated Attribute is used to indicate types and types of types that should not be used longer” - this is your use case.

Edit: I highly recommend eliminating the exception in the accessory. This violates what is commonly called the "Liskov Substitution Principle" - that you can use a derived class wherever a base class is required. Indeed, it's just a fancy language around the fact that existing code that you could pass to a derived instance expects the .Items getter .Items function in a reasonable way, and not explode on contact. If you throw an exception, it can be difficult for you to debug errors in very inaccessible places (given that your superclass is a black box).

+2
source

You can make the property public in the child class and throw an exception if it is available. You need to make sure that you only get access to the base class in the child class.

 public new ComboBoxItemCollection Items { get { throw new InvalidOperationException(); } } 
+1
source

All Articles