The essence of the problem is, given a class hierarchy like this:
class A { protected void MethodToExpose() {} protected void MethodToHide(object param) {} } class B : A { new private void MethodToHide(object param) {} protected void NewMethodInB() {} } class C : B { public void DoSomething() { base.MethodToHide("the parameter"); // This still calls A.MethodToHide() base.MethodToExpose(); // This calls A.MethodToExpose(), but that's ok base.NewMethodInB(); } }
How can I prevent any classes that inherit from class "B" from seeing the method A.MethodToHide()
? In C++, this was easy enough by using a declaration such as class B : private A
, but this syntax is not valid in C#.
For those interested (or wondering what I'm really trying to do), what we're trying to do is create a wrapper for for Rhino.Commons.NHRepository that hides the methods we don't want to expose to our group of developers, so we can have a cookie-cutter way of developing our app that new developers can easily follow. So yes, I believe the "Is-A" test is valid for the whole chain (WidgetRepository Is-A BaseRepository Is-A NHRepository).
Edit: I should have mentioned, for the sake of argument, that class A is an API class outside of our control. Otherwise the problem gets considerably easier.
It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method. If you do not use the new keyword, then the compiler will automatically hide the method of the base class.
The "override" modifier extends the base class method, and the "new" modifier hides it. The "virtual" keyword modifies a method, property, indexer, or event declared in the base class and allows it to be overridden in the derived class.
No inherited members can be removed, ever. This is not how inheritance works. You can override a virtual method or virtual getter or setter of a property. Also, quite simply, you can hide an inherited member by a new member of the same type, but the inherited member will always be there and accessible.
Obsolete It
In class B, override MethodToHide and add the Obsolete attribute
[Obsolete("Reason", true)] // true will cause a compile-time error
Set EditorBrowsable
(As mentioned previously)
In class B, override MethodToHide and add the EditorBrowsable attribute
[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
Throw exception
(As mentioned previously)
In class B, override MethodToHide and throw exception.
Create Wrapper
I think Michael Meadows is right. Use the Adapter pattern. This pattern also allows easier mocking of code when unit testing.
class B: IInterface { protected void MethodToExpose() { A a = new A(); a.MethodToExpose(); } protected void NewMethodInB() { } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With